如何正确链接和构建动态链接库?

6
我在Linux系统中。这是我的Makefile文件:
main2: main.cpp
g++ -c $(LIBS) $(CFLAGS) -fPIC main.cpp
g++ -shared main.o -o main.so 

Where,

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库链接main.os文件时出现了问题。
from ctypes import * import os
libtest = cdll.LoadLibrary(os.getcwd()+ '/main.so') libtest.main_loop()
libtest.main_loop()

我遇到了这个错误。
>>> 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++,并收到了以下错误信息:

 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'

....


构建文件 - 我想你的意思是 Makefile - Nathan Osman
2个回答

11

首先需要注意的是,如果不构建共享库,调试将更加容易。你正在使用-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中,您必须始终在目标文件之后放置库文件,因此我相应地更改了makefile的最后一行。


5
很高兴听到这个消息。您可以勾选“接受答案”复选框以标记问题已解决吗?谢谢。 - mgiuca
我点击了被接受的答案框并点赞了。很抱歉,我忘记做了这件事。 - HaltingState
@HaltingState 嘿,别担心。我很高兴你在一年多之后回来了。因为你在那么多人投票支持后选择了它,所以我得到了一个徽章(“启迪”)。另外请注意,我刚刚编辑了答案,因为我后来发现你需要在.o文件后面放置libs。 - mgiuca

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

而且您可能不想创建so文件,而是普通的可执行文件。


他确实想创建一个so文件,因为他要将其导入到Python中。 - mgiuca

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接