使用 C++ 的 SOIL 库链接器错误问题
Posted
技术标签:
【中文标题】使用 C++ 的 SOIL 库链接器错误问题【英文标题】:Trouble with SOIL library linker error using C++ 【发布时间】:2016-05-19 20:09:18 【问题描述】:(您好,这是我的第一篇文章,我使用的是 Dev C++ IDE 5.11) 我包括了头文件 SOIL.h 和链接器:-lSOIL。 该程序编译没有问题并且运行正常,但是当我将任何函数从 SOIL 库添加到我的代码中时,我收到了链接器错误。 '目标 'Project1.exe' 的配方失败' 以下是来自 SOIL 的导致链接器错误的代码:
unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
(对此可能有一个简单的解释和答案,但我到处搜索,因为找不到。) 注意:我正在学习网站 learnopengl 的教程 这是我的 makefile.win:
# Project: Project1
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = Untitled2.o
LINKOBJ = Untitled2.o
LIBS = -L"D:/Dev-Cpp/MinGW32/lib" -L"D:/Dev-Cpp/MinGW32/mingw32/lib" -static-libstdc++ -static-libgcc -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -lopengl32 -lopengl32 -lmingw32 -lSOIL
INCS = -I"D:/Dev-Cpp/MinGW32/include" -I"D:/Dev-Cpp/MinGW32/lib/gcc/mingw32/4.7.2/include"
CXXINCS = -I"D:/Dev-Cpp/MinGW32/include" -I"D:/Dev-Cpp/MinGW32/lib/gcc/mingw32/4.7.2/include" -I"D:/Dev-Cpp/MinGW32/lib/gcc/mingw32/4.7.2/include/c++"
BIN = Project1.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
$RM $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
Untitled2.o: Untitled2.cpp
$(CPP) -c Untitled2.cpp -o Untitled2.o $(CXXFLAGS)
编译日志:
Compiling project changes...
--------
- Project Filename: D:\Desktop Files\Programming Stuff\C++\C++ Projects\project examples\etchasketch\Project1.dev
- Compiler Name: MinGW GCC 4.7.2 32-bit Release
Building makefile...
--------
- Filename: D:\Desktop Files\Programming Stuff\C++\C++ Projects\project examples\etchasketch\Makefile.win
Processing makefile...
--------
- Makefile Processor: D:\Dev-Cpp\MinGW32\bin\mingw32-make.exe
- Command: mingw32-make.exe -f "D:\Desktop Files\Programming Stuff\C++\C++ Projects\project examples\etchasketch\Makefile.win" all
g++.exe Untitled2.o -o Project1.exe -L"D:/Dev-Cpp/MinGW32/lib" -L"D:/Dev-Cpp/MinGW32/mingw32/lib" -static-libstdc++ -static-libgcc -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -lopengl32 -lmingw32 -Bstatic -lSOIL
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x3e): undefined reference to `glGetString@4'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x72): undefined reference to `glGetString@4'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0xbe): undefined reference to `glGetString@4'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0xf2): undefined reference to `glGetString@4'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x115): undefined reference to `glGetString@4'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x15e): more undefined references to `glGetString@4' follow
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x1d1): undefined reference to `wglGetProcAddress@4'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x5e2): undefined reference to `glTexImage2D@36'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x66a): undefined reference to `glTexImage2D@36'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x84f): undefined reference to `glDeleteTextures@8'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0xdf8): undefined reference to `glReadPixels@28'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x100d): undefined reference to `glGetIntegerv@8'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x1427): undefined reference to `glTexImage2D@36'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x15a8): undefined reference to `glTexImage2D@36'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x161a): undefined reference to `glTexImage2D@36'
D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x188c): undefined reference to `glTexImage2D@36'
d:/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: D:/Dev-Cpp/MinGW32/lib/libSOIL.a(SOIL.o): bad reloc address 0x0 in section `.data'
d:/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
D:\Desktop Files\Programming Stuff\C++\C++ Projects\project examples\etchasketch\Makefile.win:25: recipe for target 'Project1.exe' failed
mingw32-make.exe: *** [Project1.exe] Error 1
Compilation results...
--------
- Errors: 1
- Warnings: 0
- Compilation Time: 0.25s
【问题讨论】:
'recipe for target 'Project1.exe' failed' 信息量不是很大,你能发布一个更早的错误吗?此外,检查 libSOIL 是否位于 LIBS= 行的 /lib 目录之一中。 我得到的唯一其他错误是:id 返回了 1 个退出状态。不幸的是,没有更早的错误。是应该在 /lib 目录中的 libsoil.a 吗?这就是现在里面的东西。 尝试-lsoil
而不是-lSOIL
。 .a 也是一个静态库,尝试在-lsoil
之前添加-Wl,-Bstatic
。
好的。我试过 -lsoil 但失败了。然后我添加了-wl 和-Bstatic。这给了我错误:[错误]无法识别的命令行选项'-Wl'
如果它与 ld 而不是 gcc 链接,也许只是 -Bstatic
。或添加-static
以静态链接所有内容。
【参考方案1】:
非常感谢帕特里克·斯图尔特帮助我解决这个问题。 奇怪的是,当我没有 -Bstatic 时,只有一个错误。当我按照您所说的有关编译日志的内容进行操作时,它给出了更多我不知道的错误。我用谷歌搜索并从这个网站找到了答案:http://www.gamedev.net/topic/614336-soil-linker-errors/ -lSOIL 必须在 -lopengl32 之前。 在我看来这很奇怪,但这是一个解决方案。
【讨论】:
以上是关于使用 C++ 的 SOIL 库链接器错误问题的主要内容,如果未能解决你的问题,请参考以下文章