简体   繁体   English

链接未在Makefile中完成

[英]Linking not done in Makefile

I tried to make a Makefile using files main.cpp, factorial.cpp, hello.cpp and function.h On typing 'make' on Linux command window, it shows: 我尝试使用文件main.cpp,factorial.cpp,hello.cpp和function.h制作一个Makefile。在Linux命令窗口输入'make',它显示:

g++ -c -o hello main.o factorial.o hello.o
g++: main.o: linker input file unused because linking not done
g++: factorial.o: linker input file unused because linking not done
g++: hello.o: linker input file unused because linking not done

I am making the Makefile for first time. 我是第一次制作Makefile。 Please give suggestions what can be the problem? 请提出建议可能是什么问题? The Makefile contains following code-> Makefile包含以下代码 - >

hello: main.o factorial.o hello.o
        g++ -c -o hello main.o factorial.o hello.o

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

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

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

The individual file contents if you want to see are: 1) main.cpp 如果你想看到的单个文件内容是:1)main.cpp

#include<iostream>
#include"functions.h"
using namespace std;
int main()
{
    print_hello();
    cout << endl;
    cout << "The factorial of 5 is " << factorial(5) << endl;
    return 0;
}

2) hello.cpp 2)hello.cpp

#include<iostream>
#include "functions.h"
using namespace std;

void print_hello()
{
    cout << "Hello World!";
}

3) factorial.cpp 3)factorial.cpp

#include "functions.h"

int factorial(int n)
{
    if(n!=1)
    {
        return(n * factorial(n-1));
    }
    else return 1;
}

4) function.h 4)function.h

 void print_hello();  
 int factorial(int n);

The -c parameter to g++ tells it not to link: g++-c参数告诉它不要链接:

-c Compile or assemble the source files, but do not link. -c编译或汇编源文件,但不链接。 The linking stage simply is not done. 链接阶段根本没有完成。 The ultimate output is in the form of an object file for each source file. 最终输出是每个源文件的目标文件的形式。

You definitely don't want the -c here: 你绝对不希望-c在这里:

hello: main.o factorial.o hello.o
        g++ -c -o hello main.o factorial.o hello.o

You could also use rules and patterns to make it more generic: 您还可以使用规则和模式使其更通用:

SRC_DIR = ./src
OBJ_DIR = ./bin/obj
BIN_DIR = ./build/bin

# List all the sources
SRCS = A.cpp B.cpp

# Define the rule to make object file from cpp
$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp
    g++ -o $@ $(INCLUDES) $(CPPFLAGS) -c $^

TARGET_BIN = $(BIN_DIR)/test

all : make_dirs $(TARGET_BIN)

$(TARGET_BIN) : $(SRCS:%.cpp=$(OBJ_DIR)/%.o)
    g++ $(LDFLAGS) -o $@ $^ $(LDLIBS)

make_dirs :
    mkdir -p $(OBJ_DIR)
    mkdir -p $(BIN_DIR)

With this approach you have several benefits: 使用这种方法,您有几个好处:

  • Easy to use: you specify source files once, and you don't care about processing of each object file: the job is done by a single rule. 易于使用:您只需指定一次源文件,而不关心每个目标文件的处理:作业由单个规则完成。

  • More maintainable: every time you need to change the compiler or linker options you do it in the single rule, not for each translation unit. 更易于维护:每次需要更改编译器或链接器选项时,都要在单个规则中执行,而不是每个转换单元。

You have an error in that line: 你在该行中有错误:

    g++ -c -o hello main.o factorial.o hello.o

man gcc says: ... the -c option says not to run the linker. man gcc说: ... the -c option says not to run the linker.

This option is used to produce objects files only. 此选项仅用于生成对象文件。 When it passed, gcc will not start linker to produce executable file or shared library. 当它通过时,gcc不会启动链接器来生成可执行文件或共享库。

As clear from all above answers, because of using -c in g++ -c -o hello main.o factorial.o hello.o , it is preventing from linking. 从以上所有答案中可以清楚地看出,由于在g++ -c -o hello main.o factorial.o hello.o中使用-c ,因此阻止了链接。 After creating the object files from corresponding .cpp or .h etc files, these need to be linked. 从相应的.cpp或.h等文件创建目标文件后,需要将它们链接起来。 Without linking, as each file is a part of a complete program that can perform some task, nothing useful can be done as these file are dependent. 没有链接,因为每个文件都是可以执行某些任务的完整程序的一部分,所以没有什么用处可以完成,因为这些文件是相关的。 So, we have to link these dependent parts to run our complete program. 因此,我们必须链接这些相关部分来运行我们的完整程序。

One basic video tutorial for easy learning of making a Makefile in the two methods viz dependencies and pattern rules is here . 在这两种方法即依赖性和模式的规则作出的Makefile容易学习一个基本的视频教程是在这里 It takes example of method of dependencies and then introduce pattern rules as a better approach for making a long Makefile. 它采用依赖方法的示例,然后引入模式规则作为制作长Makefile的更好方法。


To know the difference between compilation and linking, this link can be useful. 要了解编译和链接之间的区别,此链接可能很有用。

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

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