2010-11-29 130 views
6

我在linux中。我的Makefile文件是這樣的如何正確鏈接和構建動態鏈接庫?

main2: main.cpp 
g++ -c $(LIBS) $(CFLAGS) -fPIC main.cpp 
g++ -shared main.o -o main.so 

其中,

SDL_CFLAGS := $(shell sdl-config --cflags) 
SDL_LDFLAGS := $(shell sdl-config --libs) 
CC = gcc 
COPTS = -g -Wall 
CFLAGS = $(SDL_CFLAGS) 
LIBS = -lstdc++ -lSDL $(SDL_LDFLAGS) -L/usr/X11R6/lib -lGL -lGLU 

它運行

g++ -c -lstdc++ -lSDL -L/usr/lib -lSDL -L/usr/X11R6/lib -lGL -lGLU -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -fPIC main.cpp 

g++ -shared main.o -o main.so 

現在,這個工作沒有錯誤。生成main.o和main.so文件。

然而,當我嘗試使用python的ctypes

from ctypes import * import os 
libtest = cdll.LoadLibrary(os.getcwd()+ '/main.so') libtest.main_loop() 
libtest.main_loop() 

鏈接main.os我得到這個錯誤

>>> libtest = cdll.LoadLibrary(os.getcwd() + '/main.so') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.6/ctypes/__init__.py", line 431, in LoadLibrary 
    return self._dlltype(name) 
    File "/usr/lib/python2.6/ctypes/__init__.py", line 353, in __init__ 
    self._handle = _dlopen(self._name, mode) 
OSError: /home/atomos/DF/open_gl_client/ctypes_client/main.so: undefined symbol: glEnd 

我不知道如果我正確地創建鏈接庫。我如何創建一個可以加載的鏈接庫?

是否必須爲從main.cpp導入的每個庫創建.o和.os文件,還是自動處理?

我不明白編譯器或鏈接器在做什麼,但它適用於沒有導入的簡單示例,但對於導入opengl庫的cpp文件,它會出現該錯誤。

---- ----更新針對main.so產量 LDD

ldd main.so 
     linux-gate.so.1 => (0xb7755000) 
     libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xaf625000) 
     libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xaf5ff000) 
     libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xaf4a4000) 
     libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xaf485000) 
     /lib/ld-linux.so.2 (0xb7756000) 

---- ----更新

我跑克++而不在第二-shared標誌編譯步驟和收到此錯誤

g++ main.o -o main.so 
main.o: In function `Texture_map_list::add_texture(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
main.cpp:(.text+0xf0fe): undefined reference to `glGenTextures' 
main.cpp:(.text+0xf2ba): undefined reference to `glBindTexture' 
main.cpp:(.text+0xf2d7): undefined reference to `glTexParameterf' 
main.cpp:(.text+0xf2f4): undefined reference to `glTexParameterf' 
main.cpp:(.text+0xf310): undefined reference to `glTexParameteri' 
main.cpp:(.text+0xf32c): undefined reference to `glTexParameteri' 
main.cpp:(.text+0xf365): undefined reference to `gluBuild2DMipmaps' 
main.o: In function `init()': 
main.cpp:(.text+0xf457): undefined reference to `SDL_Init' 
main.cpp:(.text+0xf46d): undefined reference to `SDL_WM_SetCaption' 
main.cpp:(.text+0xf472): undefined reference to `SDL_GetVideoInfo' 
main.cpp:(.text+0xf480): undefined reference to `SDL_GetError' 
main.cpp:(.text+0xf497): undefined reference to `SDL_Quit' 
main.cpp:(.text+0xf4e2): undefined reference to `SDL_GL_SetAttribute' 
main.cpp:(.text+0xf505): undefined reference to `SDL_SetVideoMode' 
main.cpp:(.text+0xf513): undefined reference to `SDL_GetError' 
main.cpp:(.text+0xf52a): undefined reference to `SDL_Quit' 
main.cpp:(.text+0xf559): undefined reference to `glClearColor' 
main.cpp:(.text+0xf565): undefined reference to `glEnable' 
main.cpp:(.text+0xf571): undefined reference to `glMatrixMode' 
main.cpp:(.text+0xf576): undefined reference to `glLoadIdentity' 
main.cpp:(.text+0xf5a2): undefined reference to `gluPerspective' 
main.o: In function `process_keypresses()': 
main.cpp:(.text+0x10678): undefined reference to `SDL_PollEvent' 
main.cpp:(.text+0x109a1): undefined reference to `SDL_PollEvent' 
main.o: In function `main_loop': 
main.cpp:(.text+0x10d76): undefined reference to `SDL_GetKeyState' 
main.cpp:(.text+0x10d9f): undefined reference to `SDL_Quit' 
main.o: In function `render()': 
main.cpp:(.text+0x10e00): undefined reference to `SDL_GetMouseState' 
main.cpp:(.text+0x10e90): undefined reference to `SDL_GetMouseState' 
main.cpp:(.text+0x10f94): undefined reference to `glClear' 
main.cpp:(.text+0x10fa0): undefined reference to `glMatrixMode' 
main.cpp:(.text+0x10fa5): undefined reference to `glLoadIdentity' 
main.cpp:(.text+0x11081): undefined reference to `gluLookAt' 
main.cpp:(.text+0x11120): undefined reference to `glTranslatef' 
main.cpp:(.text+0x1114d): undefined reference to `glRotatef' 
main.cpp:(.text+0x1117a): undefined reference to `glRotatef' 
main.cpp:(.text+0x1119e): undefined reference to `SDL_GL_SwapBuffers' 
main.o: In function `draw_plane(float, float, float)': 
main.cpp:(.text+0x111d8): undefined reference to `glColor3f' 
main.cpp:(.text+0x111e4): undefined reference to `glBegin' 
main.cpp:(.text+0x1120b): undefined reference to `glVertex3f' 

....

+0

「構建文件」 - 我想你的意思是* Makefile *。 – 2010-11-29 01:36:07

回答

11

首先,請注意THI如果不建立共享庫,s更容易調試。您正在使用-shared進行構建,因此在使用Python動態加載庫之前,鏈接器錯誤不會發生。關閉,而你正在調試-shared,你會看到在命令行中的錯誤,當您試圖鏈接:

g++ main.o -o main 
main.o: In function `main': 
main.cpp:(.text+0x1d): undefined reference to `glBegin' 
main.cpp:(.text+0x22): undefined reference to `glEnd' 
collect2: ld returned 1 exit status 

現在的問題是,你是通過連接參數的編譯器。我發現在Makefile中你已經很好地分開了CFLAGSLIBS。好。但是你在第一行通過$(LIBS)$(CFLAGS)來構建main.o. LIBS將在該行被忽略,因爲它們是鏈接標誌。

在第二行,實際構建最終可執行文件的位置,您不通過$(LIBS),因此該程序未與libGL或任何其他庫鏈接。

因此,簡單地解決您的makefile正是如此:

main2: main.cpp 
    g++ -c $(CFLAGS) -fPIC main.cpp 
    g++ -shared main.o $(LIBS) -o main.so 

編輯:因爲我已經意識到,在GCC,你必須always put the libraries after the object files,所以我相應改變Makefile文件的最後一行。

+0

這解決了這個問題。謝謝。 – HaltingState 2010-11-29 01:55:37

+5

好聽。你能勾選'接受答案'複選框來標記這個解決方案嗎?謝謝。 – mgiuca 2010-11-29 02:22:31

1

簡單:

您正在將鏈接標誌傳遞給對象編譯步驟。

這裏是你需要的東西:

g++ -c -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT main.cpp 
g++ -lstdc++ -lSDL -L/usr/lib -lSDL -L/usr/X11R6/lib -lGL -lGLU -fPIC main.o -o main 

另外,你可能不希望創建這樣的文件,而是一個正常的可執行文件。