gpt4 book ai didi

c++ - 无法让 GLFW 链接

转载 作者:可可西里 更新时间:2023-11-01 18:26:37 28 4
gpt4 key购买 nike

我正在尝试编译以下测试程序:

#include <GL/glfw.h>

int main(int argc, char** argv) {
if(!glfwInit()) {
return -1;
}

if(!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW)) {
return -1;
}

while(glfwGetWindowParam(GLFW_OPENED)) {
glfwSwapBuffers();
}

return 0;
}

但我总是遇到关于 GLFW 函数的undefined reference错误。

下面是我的makefile:

CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDFLAGS = -lglfw

OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include

SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello

all: init $(OBJECTS) $(EXECUTABLE)

$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS)

$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $@

init:
@mkdir -p "$(OBJ_DIR)"

clean:
@rm -rf $(OBJ_DIR) $(EXECUTABLE)

我肯定有 glfw.hlibglfw.a/.so 当我运行 locate glfw 我得到:

:~$ locate glfw
/usr/include/GL/glfw.h
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/libglfw.so.2
/usr/lib/libglfw.so.2.6

nm/usr/lib/libglfw.a 的输出 | grep glfwInit:

:~$ nm /usr/lib/libglfw.a | grep glfwInit
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
0000000000000000 B _glfwInitialized
0000000000000000 T glfwInit
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitJoysticks
U _glfwInitTimer
00000000000000c0 T _glfwInitJoysticks
0000000000000000 T _glfwInitTimer

以及来自 clang 的详细消息:

clang++ -I/usr/include -c main.cpp -o bin/main.o
clang++ -Wall -std=c++0x -Wl --verbose -lglfw -lGL -lGLU -L/usr/lib -o hello bin/main.o
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang: warning: argument unused during compilation: '-std=c++0x'
"/usr/bin/ld" -z relro --hash-style=gnu --as-needed --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. -L/lib/x86_64-linux-gnu -L/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib -lglfw -lGL -lGLU bin/main.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
bin/main.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `glfwInit'
main.cpp:(.text+0x76): undefined reference to `glfwOpenWindow'
main.cpp:(.text+0x97): undefined reference to `glfwGetWindowParam'
main.cpp:(.text+0xa7): undefined reference to `glfwSwapBuffers'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1

好像没有找到图书馆?

最佳答案

问题是 glfw 库在依赖于它们的目标文件之前被指定给链接器。 ld 搜索库以仅针对它在其正在处理的文件列表中知道的依赖项来解析依赖项。因此,当 ld 搜索 libglfw.a 时,它还不知道 main.o 中的 glfwInit 依赖项. ld(默认情况下)不会返回并再次搜索库。

尝试:

$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDFLAGS)

此外,库可能应该在 LDLIBS(或 LIBS)变量中指定 - LDFLAGS 通常用于链接器选项:

CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDLIBS = -lglfw -lGL -lGLU

OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include

SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello

all: init $(OBJECTS) $(EXECUTABLE)

$(EXECUTABLE):
$(CXX) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDLIBS)

$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $@

init:
@mkdir -p "$(OBJ_DIR)"

clean:
@rm -rf $(OBJ_DIR) $(EXECUTABLE)

关于c++ - 无法让 GLFW 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16503527/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com