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

标签 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

我收到一堆 undefined reference 错误:

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

最佳答案

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

关于c++ - 使用 g++ 链接对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468113/

相关文章:

c++ - 为什么内联声明不是不完整的类型?

java - Libgdx 的 Matrix4#translate() 无法按预期工作

c++ - 使用opengl C++拖放图像

c++ - 在 32 位和 64 位程序中使用 std::chrono::duration::rep 和 printf

python - 如何将构建的 Cython 扩展从一台 PC 转移到另一台?

linux - .zshrc 中最有效的 if 语句来检查 Linux 操作系统是否在 WSL 上运行?

c - Makefile - 通配符和配方构建

c++ - 为什么 Sprite 不在 OpenGL 中渲染?

c++ - MSVC 2013 在 x64 构建中加载 x86 系统库

c++ - 命令使用 shell 运行但卡在 QProcess 中