make:隐式规则来链接c ++项目 [英] make: implicit rule to link c++ project

查看:120
本文介绍了make:隐式规则来链接c ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个make教程。非常简单的测试项目我试图构建只有3个文件: ./ src / main.cpp ./src/implementation.cpp ./ include / header.hpp 这是make文件。

  VPATH = src include 
CPPFLAGS = -I include

main:main.o implementation.o
main.o:header.hpp
implementation.o:header.hpp

当没有任何参数调用 make 只构建目标文件,但不链接可执行文件。应该有 prog 的隐含规则,或者我遗漏了什么?我真的需要有人指向正确的方向。



谢谢。



与源文件的前缀相同。现在拨打 cc 可以链接对象文件。

  g ++ -I include -c -o main。 o src / main.cpp 
g ++ -I include -c -o implementation.o src / implementation.cpp
cc main.o implementation.o -o main



由于某种原因,与g ++的链接工作,但与cc关联不会。



我可以明确地指定规则,但是想要学习如何使用隐式规则。

解决方案

根据制作手册,您可以使用隐式链接规则与多个对象如果其中之一与可执行文件名称匹配,例如:

  VPATH = src include 
CPPFLAGS = -I include

main:implementation.o
main.o:header.hpp
implementation.o:header.hpp

这将从main.o和implementation.o中构建一个名为main的可执行文件。



注意,规则使用C编译器进行链接,默认情况下不会链接到C ++ std库,您需要将它明确添加到LDFLAGS中


I am working my way through a make tutorial. Very simple test projects I am trying to build has only 3 files: ./src/main.cpp ./src/implementation.cpp and ./include/header.hpp This is the make file.

VPATH = src include
CPPFLAGS = -I include

main: main.o implementation.o
main.o: header.hpp
implementation.o: header.hpp

When called without any arguments make builds only object files, but doesn't link the executable file. There supposed to be an implicit rule for prog or I am missing something? I really need someone to point me into right direction.

Thanks.

I made the first target name the same as the prefix of the source file. Now make calls cc to link object files.

g++  -I include  -c -o main.o src/main.cpp    
g++  -I include  -c -o implementation.o src/implementation.cpp
cc   main.o implementation.o   -o main

For some reason linking with g++ works, but linking with cc doesn't.

I could specify the rule explicitly, but want to learn how to use implicit rules.

解决方案

According to the make manual, you can use the implicit linking rule with multiple objects if one of these matches the executable name, eg:

VPATH = src include
CPPFLAGS = -I include

main: implementation.o
main.o: header.hpp
implementation.o: header.hpp

This will build an executable named main from both main.o and implementation.o.

Note however that the builtin implicit rule uses the C compiler for linking, which will not link against the C++ std library by default, you will need to add this to LDFLAGS explicitly

这篇关于make:隐式规则来链接c ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆