【问题标题】:MinGW32 linking error - builds in one command, but fails using MakefileMinGW32 链接错误 - 在一个命令中构建,但使用 Makefile 失败
【发布时间】:2016-07-29 16:17:18
【问题描述】:

我遇到的问题与构建 GLFW 示例有关。在命令提示符中,如果我尝试使用一行(如下所示)构建程序,那么它可以工作。

g++ -g -Wall -Ideps/include/ main.cpp -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe

但是我想使用下面的 Makefile 来构建它。

PROG = test.exe
CC = g++
CPPFLAGS = -g -Wall -Ideps/include/
LDFLAGS = -Ldeps/lib/ -lglfw3
LDFLAGS += -lopengl32 -lgdi32
OBJS = main.o

$(PROG) : $(OBJS)
    $(CC) $(LDFLAGS) -o $(PROG) $(OBJS)

main.o :
    $(CC) $(CPPFLAGS) -c main.cpp

但是当我这样做时,我遇到了以下错误:

main.o: In function `main':
C:\Users\Kieran\GLFWtest/main.cpp:9: undefined reference to `glfwSetErrorCallback'
C:\Users\Kieran\GLFWtest/main.cpp:10: undefined reference to `glfwInit'
C:\Users\Kieran\GLFWtest/main.cpp:12: undefined reference to `glfwCreateWindow'
C:\Users\Kieran\GLFWtest/main.cpp:15: undefined reference to `glfwTerminate'
C:\Users\Kieran\GLFWtest/main.cpp:18: undefined reference to `glfwMakeContextCurrent'
C:\Users\Kieran\GLFWtest/main.cpp:19: undefined reference to `glfwSwapInterval'
C:\Users\Kieran\GLFWtest/main.cpp:20: undefined reference to `glfwSetKeyCallback'
C:\Users\Kieran\GLFWtest/main.cpp:25: undefined reference to `glfwGetFramebufferSize'
C:\Users\Kieran\GLFWtest/main.cpp:27: undefined reference to `_imp__glViewport@16'
C:\Users\Kieran\GLFWtest/main.cpp:28: undefined reference to `_imp__glClear@4'
C:\Users\Kieran\GLFWtest/main.cpp:29: undefined reference to `_imp__glMatrixMode@4'
C:\Users\Kieran\GLFWtest/main.cpp:30: undefined reference to `_imp__glLoadIdentity@0'
C:\Users\Kieran\GLFWtest/main.cpp:31: undefined reference to `_imp__glOrtho@48'
C:\Users\Kieran\GLFWtest/main.cpp:32: undefined reference to `_imp__glMatrixMode@4'
C:\Users\Kieran\GLFWtest/main.cpp:33: undefined reference to `_imp__glLoadIdentity@0'

还有一些错误,但它们只是更多未定义的引用。我尝试通过手动调用以下两个命令来运行 Makefile,一个是编译,一个是链接。它编译得很好,但在链接时会出现与上面相同的错误,

g++ -g -Wall -Ideps/include/ -c main.cpp
g++ -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe main.o

我不知道为什么在一行中构建它是可行的,但是在使用我认为本质上是相同的过程时,编译然后链接会失败。我对 Makefiles 比较陌生,所以我可能会犯一些新手错误,但我的搜索未能产生任何答案。我还想指出,在我的 Mac 上使用类似的 Makefile 构建没有任何问题。

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 您显示的手动命令与makefile将运行的命令有很大不同:命令行中库的位置。
  • 尝试将.o 文件放在-l* 标志之前。像这样:g++ main.o -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe。并相应地修改makefile。
  • 好的,谢谢,成功了。最好先使用相关文件(即 main.cpp 或 main.o)订购命令,然后再使用包含和库标志?
  • 传统上是在之前标记,之后是库。这意味着您需要将当前的 LDFLAGS 变量拆分为例如LDFLAGSLIBS。但除了库之外,标志是在目标文件之前还是之后并不重要。

标签: c++ makefile mingw mingw32


【解决方案1】:

尝试将.o 文件放在之前 -l* 标志。

像这样:g++ main.o -Ldeps/lib/ -lglfw3 -lopengl32 -lgdi32 -o test.exe

并以同样的方式修改makefile。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2019-04-28
    • 2013-01-27
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多