gpt4 book ai didi

c++ - makefile c++ 与其他 h 文件依赖与 OpenGL

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:12 25 4
gpt4 key购买 nike

我这辈子都无法使用 makefile 正确编译它我有使用 OpenGL 的 C++ 文件。

一些文件依赖于其他文件

我有5个文件

  • main.cpp
  • Vector.h
  • vector .cpp
  • 实用程序.cpp
  • Utility.h

~ 依赖关系~

在 MAIN.CPP 中

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "Vector.h"

在 vector 中.CPP

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "Vector.h"
#include "Utility.h"

实用工具.CPP

#include "Utility.h"

我在尝试并排列各种标志之后就是这样makefile 当前看起来像

program1: main.o Vector.o Utility.o
g++ -std=c++11 main.o Vector.o Utility.o -o program1
main.o: main.cpp Vector.h
g++ -std=c++11 main.cpp -lglut -lGL -lGLU -lglut -lm
Vector.o: Vector.cpp Vector.h Utility.h
g++ -std=c++11 Vector.cpp -lglut -lGL -lGLU -lglut -lm
Utility.o: Utility.cpp Utility.h
g++ -std=c++11 Utility.cpp -lglut -lGL -lGLU -lglut -lm
clean:
-rm -f *.o

键入“make”输出以下结果:

g++ -std=c++11 main.cpp -lglut -lGL -lGLU -lglut -lm
g++ -std=c++11 Vector.cpp -lglut -lGL -lGLU -lglut -lm
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In (.text+0x20): undefined reference to 'main'
collect2: error: ld returned 1 exit status
makefile:6: recipe for target 'Vector.o' failed
make: ***[Vector.o] Error 1

如何修复我的 makefile?谢谢

最佳答案

  1. 您通过省略 -c 告诉编译器链接单个目标文件
  2. 您正在将库链接到目标文件,您只需要在编译完所有目标文件后链接一次
  3. Make 内置了规则和方法,请使用它们。
objects := main.o Vector.o Utility.o
CXXFLAGS := -std=c++11

program1: CC := $(CXX)
program1: LDLIBS := -lglut -lGL -lGLU -lglut -lm
program1: $(objects)
$(LINK.o) $^ $(LDLIBS) -o $@

main.o: Vector.h
Vector.o: Vector.h Utility.h
Utility.o: Utility.h

.PHONY: clean
clean:
$(RM) $(objects)

如果将 main.o/main.cpp 更改为 program1.o/program1.cpp 你可以通过完全摆脱 program1 配方来进一步简化事情。

关于c++ - makefile c++ 与其他 h 文件依赖与 OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41798342/

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