使用g ++链接对象时出现问题

时间:2012-05-06 04:31:57

标签: c++ linux opengl g++ sdl

我正在尝试用c ++中的opengl创建一个简单的obj加载器。 使用一个命令编译所有内容工作正常,

g++ -o main main.cpp timer.cpp screen.cpp obj_loader.cpp `sdl-config --cflags --libs` `pkg-config --cflags --libs glu`

不会返回错误。

单独编译对象也可以正常工作,

g++ main.cpp -o main.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs`
g++ obj_loader.cpp -o obj_loader.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs`
g++ timer.cpp -o timer.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs`
g++ screen.cpp -o screen.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs`

也不会返回错误。

但是,在运行最终的

g++ main.o obj_loader.o timer.o screen.o -o main

我得到了一堆未定义的引用错误:

main.o: In function `draw()':
main.cpp:(.text+0x1d): undefined reference to `glColor3f'
main.cpp:(.text+0x27): undefined reference to `glBegin'
main.cpp:(.text+0x36): undefined reference to `glVertex2i'
main.cpp:(.text+0x45): undefined reference to `glVertex2i'
main.cpp:(.text+0x54): undefined reference to `glVertex2i'
main.cpp:(.text+0x63): undefined reference to `glVertex2i'
main.cpp:(.text+0x68): undefined reference to `glEnd'
main.o: In function `main':
main.cpp:(.text+0xf8): undefined reference to `SDL_PollEvent'
main.cpp:(.text+0x10b): undefined reference to `glClear'
main.cpp:(.text+0x115): undefined reference to `SDL_GL_SwapBuffers'
main.cpp:(.text+0x11a): undefined reference to `glFinish'
main.cpp:(.text+0x14e): undefined reference to `SDL_Delay'
timer.o: In function `Timer::start()':
timer.cpp:(.text+0x4d): undefined reference to `SDL_GetTicks'
timer.o: In function `Timer::pause()':
timer.cpp:(.text+0xa6): undefined reference to `SDL_GetTicks'
timer.o: In function `Timer::unpause()':
timer.cpp:(.text+0xe5): undefined reference to `SDL_GetTicks'
timer.o: In function `Timer::tick()':
timer.cpp:(.text+0x136): undefined reference to `SDL_GetTicks'
timer.o: In function `Timer::get_ticks()':
timer.cpp:(.text+0x172): undefined reference to `SDL_GetTicks'
screen.o: In function `init()':
screen.cpp:(.text+0xa): undefined reference to `SDL_Init'
screen.cpp:(.text+0x31): undefined reference to `SDL_SetVideoMode'
screen.cpp:(.text+0x64): undefined reference to `SDL_WM_SetCaption'
screen.o: In function `init_GL()':
screen.cpp:(.text+0x80): undefined reference to `glClearColor'
screen.cpp:(.text+0x8a): undefined reference to `glMatrixMode'
screen.cpp:(.text+0x8f): undefined reference to `glLoadIdentity'
screen.cpp:(.text+0xc0): undefined reference to `glOrtho'
screen.cpp:(.text+0xca): undefined reference to `glMatrixMode'
screen.cpp:(.text+0xcf): undefined reference to `glLoadIdentity'
screen.cpp:(.text+0xd4): undefined reference to `glGetError'
screen.o: In function `clean_up()':
screen.cpp:(.text+0xf4): undefined reference to `SDL_Quit'
collect2: ld returned 1 exit status

我收录的图书馆是:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

#include "GL/gl.h"
#include "GL/glu.h"

和我的Makefile:

CC=g++
SDL_FLAGS=`sdl-config --cflags --libs`
GL_FLAGS=`pkg-config glu --cflags --libs`

CFLAGS=-c -Wall

FLAGS=$(CFLAGS) $(SDL_FLAGS) $(GL_FLAGS)
LDFLAGS=

SOURCES=main.cpp obj_loader.cpp timer.cpp screen.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=main

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(OBJECTS) -o $@ $(LDFLAGS)

.cpp.o:
    $(CC) $< -o $@ $(FLAGS)

clean:
    rm -f *o main

3 个答案:

答案 0 :(得分:2)

是的...... --libs归还了所有这些东西?您需要这些才能正确链接最终的可执行文件。

答案 1 :(得分:1)

sdl-configpkg-config带有两个标记:--cflags--libs。当您将源文件编译为目标文件时,需要第一个标志。在链接时需要第二个标志。当您使用单个命令编译所有内容时,两个阶段都由一个命令处理,因此您应该将--cflags--libs同时传递给sdl-configpkg-config同时。现在您已将这些阶段分开,您需要在正确的时间将正确的选项传递给sdl-configpkg-config

g++ main.cpp -o main.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags`
g++ obj_loader.cpp -o obj_loader.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags`
g++ timer.cpp -o timer.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags`
g++ screen.cpp -o screen.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags`

g++ main.o obj_loader.o timer.o screen.o -o main `sdl-config --libs` `pkg-config glu --libs`

在你的makefile中,这意味着按如下方式配置变量:(我还对你的Makefile做了一些其他的小修补)

CC=g++
LD=g++

CFLAGS=-c -Wall $(shell sdl-config --cflags) $(shell pkg-config glu --cflags)
LDFLAGS=$(shell sdl-config --libs) $(shell pkg-config glu --libs)

SOURCES=main.cpp obj_loader.cpp timer.cpp screen.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=main

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(LD) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

clean:
    rm -f *.o main

还有很多关于你的Makefile的批评 - 你可以在这里删除大部分动作行,因为GNU Make将它们内置为含义。您可能需要添加指定.o文件依赖关系的规则 - 每个.cpp文件可能包含.h个文件,并且需要知道重新编译.cpp个文件当他们依赖的.h文件发生变化时。

答案 2 :(得分:0)

我没有看到任何-llibraryname告诉g ++与opengl链接。我原本希望在构建main本身的g ++行上看到这个(即以'main'结尾的行)。据我所知,.o文件本身不包含有关在哪里找到解析其引用的库的信息。必须在命令行上以-l开关传递该信息。

相关问题