歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux文化 >> 編譯一個教老的C程序碰到的一個有關於X圖形界面的問題

編譯一個教老的C程序碰到的一個有關於X圖形界面的問題

日期:2017/2/27 12:12:26   编辑:Linux文化

/******************************************************************************** * * Purpose: Program extracts information from the X server and displays * it on the screen. * Notes: Use the following command to compile. * * gcc hardware.c -lX11 * * Author: XW. Zheng * * Date: 31-Mar-06 * ********************************************************************************/ /************************* Includes *********************************************/ /* These are in /usr/X386 */

#include #include #include

#include

main() { Display * display; int screen; char * display_name = NULL; Window root; Visual* visual; // Connect to X display server display = XOpenDisplay(display_name); // Get screen ID screen = DefaultScreen(display); printf("\n\t Information extracted from the X server\n"); printf("\t--------------------------------------\n\n"); printf(" X server by \' %s\'\n", ServerVendor(display)); printf(" X Server protocol %d\n", ProtocolVersion(display)); printf(" X server Release %d\n", VendorRelease(display)); printf(" Screen is %dmm hight.\n", DisplayHeightMM(display, screen)); printf(" Screen is %dmm wide.\n", DisplayWidthMM(display, screen)); printf(" Screen is %d pixels high.\n", DisplayHeight(display, screen)); printf(" Screen is %d pixels wide.\n", DisplayWidth(display, screen)); visual = DefaultVisual(display, screen); printf(" %3d Colour map entries", visual->map_entries); printf(" (Number of colours on the screen at one time).\n"); printf(" %3d Display planes (bits per screen pixel).\n", DisplayPlanes(display, screen)); printf(" There is %d screens(s).\n", ScreenCount(display)); }

其實這段代碼,是KDEVELPE裡面的一個C程序的EXAMPLES, 我試著對他編譯,但老是出錯, gcc -o hardware_c hardware_c.c 的時候出錯信息如下

hardware_c.c:64:2: warning: no newline at end of file /tmp/ccEJFRgk.o(.text+0x1e): In function `main': : undefined reference to `XOpenDisplay' collect2: ld returned 1 exit status

我不清楚這是什麼意思,我試著在上面的幾個頭文件中查找是否有這個 XOpenDisplay的函數, cat Xlib.h | grep XOpenDisplay 是有的,但是他為什麼說我 undefined reference 我有點不懂,

然後我又用了,documation裡跟我說的方法 gcc hardware.c -lX11 就在代碼的最上面的注釋中,有的, 但是這次出錯信息如下 hardware_c.c:64:2: warning: no newline at end of file /usr/bin/ld: cannot find -lX11 collect2: ld returned 1 exit status 我就搞不懂了, 是說我那個 -l 的參數錯了問題嗎,我 又用man gcc仔細找了找, 好象是L,所以又換了一下,但是還是不行,

說明1;這裡面的出錯信息,那個什麼 no newline at end of file 和 這個沒什麼關系,但是要是有人 跟我說一下為什麼 會有這個警告, 我會很感激, NEWLINE好象和換行有關系吧. 說明2: 不好意思,因為我在編譯的時候,有幾次那個GCC跟我說,什麼X11找不到什麼的,我覺的麻煩,就把頭文件中的相對路徑全改成絕對的了,所以,會很長, 不過這個路徑沒什麼錯誤的,我是用 locate定位到的.

不好意思,提問提太長了,但是我又短不下來,

>>> 此貼的回復 >> newline的警告是因為源文件最後沒有空行。

X11庫在什麼位置?用-L加上X11所在庫的路徑。-lX11還是要的,表示要連接該庫。

>>> 此貼的回復 >> 對不起,我想這位朋友沒有認真的看這道題, gcc hardware.c -lX11 是官方給出的寫法,我不知道為什麼不行

>>> 此貼的回復 >> 肯定是找不到庫的位置了。 你可以通過兩種方法: 1) 如feirygnu說的, 加上-L/usr/X11R6/lib (我假設你的庫的位置是這個) 2) 修改 /etc/ld.so.conf ,加入 /usr/X11R6/lib, 用ldconfig -v 刷新庫的配置

>>> 此貼的回復 >> 那謝謝了, 我在試試看, 不過,我並不認為

===================================================================== 是官方給出的寫法 === 嘿嘿,這個我給不了,需要等我做了官:) 這個世界上一模一樣的系統不多,所以官方的寫法不一定適用於所有系統。 =====================================================================

我並不需要他達到那個水平, 但是,他應該保持基本的通用性. 我想主要的原因是, 因為那個C文件太早了, 是別人 94年寫的, 他的另一個EXAMPLES,我花了六個小時才修改正確, 他原先的SOURCE 編譯以後,老 說, 段錯誤,

但還是謝謝 fierygnu和joyself, 因為我感覺是我錯了.


Copyright © Linux教程網 All Rights Reserved