简体   繁体   English

Makefile链接

[英]Makefile linking

I ran into an issue with make. 我遇到了make问题。 I have 3 files. 我有3个档案。

main.cpp | main.cpp | src/Math/Vector2.cpp | src / Math / Vector2.cpp | src/Math/Vector2.hpp src / Math / Vector2.hpp

Here is my MakeFile: 这是我的MakeFile:

main: vector2.o main.o
    g++ -o main.o vector2.o

main.o: main.cpp
    g++ -o main.o main.cpp -c

vector2.o: src/Math/Vector2.cpp src/Math/Vector2.hpp
    g++ -o vector2.o src/Math/Vector2.cpp -lm -c

When i copy these commands manually , it compiles perfectly fine. 当我手动复制这些命令时,它可以完美编译。 However $make main returns 但是$ make主要收益

g++     main.cpp   -o main
/tmp/ccnRZ4UD.o: In function `main':
main.cpp:(.text+0x42): undefined reference to ` 
phy2d::Maths::Vector2f::Vector2f(double, double)'
main.cpp:(.text+0x66): undefined reference to ` 
phy2d::Maths::Vector2f::Vector2f(double, double)'
main.cpp:(.text+0x79): undefined reference to ` 
phy2d::Maths::Vector2f::distance(phy2d::Maths::Vector2f const&) 
const'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'main' failed
make: *** [main] Error 1

Any ideas?? 有任何想法吗??

There's no way that the makefile you provide will give the output you show. 您提供的makefile不可能给您显示的输出。

In your comment you said Here is my MakeFile . 在您的评论中,您说这是我的MakeFile Note that make will not read a file named MakeFile . 请注意,make不会读取名为MakeFile的文件。 It will read files named Makefile and makefile , but if you're using a case-sensitive filesystem then one explanation for the behavior you're seeing is that you've used MakeFile for your makefile name and make can't find it. 它会读取名为Makefilemakefile ,但是如果您使用区分大小写的文件系统,那么对于所看到行为的一种解释是,您已经使用MakeFile作为您的makefile名称,而make无法找到它。

Or, you could have simply been imprecise in your question, but then this can't be the makefile that make is using for some other reason. 或者,您可能只是对问题不精确,但是由于其他原因,这可能不是make正在使用的makefile。

Also, there are numerous errors with your makefile: 另外,您的makefile中存在许多错误:

  1. You have two different targets main and main.o where the command generates the same file, -o main.o 您有两个不同的目标mainmain.o ,其中命令生成相同的文件-o main.o
  2. You are adding libraries -lm to your compile line for vector2.o ; 您正在将library -lm添加到vector2.o的编译行中; libraries should go on the link line. 库应该在链接行上。

In general you should be using automatic variables to make sure your makefile is agreeing with what you want it to do. 通常,您应该使用自动变量来确保您的makefile与您要执行的操作一致。

Here's a reasonable makefile for your situation: 这是适合您情况的合理makefile:

CXX = g++

main: vector2.o main.o
        $(CXX) -o $@ $^ -lm

main.o: main.cpp
        $(CXX) -c -o $@ $<

vector2.o: src/Math/Vector2.cpp src/Math/Vector2.hpp
        $(CXX) -c -o $@ $<

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM