简体   繁体   English

为G ++编译器创建Makefile

[英]Creating a makefile for g++ compiler

I have some code that everytime I want to compile I have to do the following command in the linux shell (just an example): 我有一些代码,每次我想编译时,都必须在linux shell中执行以下命令(仅作为示例):

$g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux64 -o sim helloworld.cpp -lsystemc -lm

In this example my source file is helloworld.cpp and I want it to be compiled in a folder called sim in the same folder where the source file is. 在此示例中,我的源文件是helloworld.cpp ,我希望将其编译在与源文件相同的文件夹中的sim文件夹中。

How can I automate this process using make or makefile . 如何使用makemakefile自动执行此过程。 Also having the make clean option to remove the created folder if called? 还具有make clean选项来删除创建的文件夹(如果调用)吗?

Easy 简单

%.o: %.cpp
     g++ $(YOUR_CXX_FLAGS_HERE) -c $< -o $@

$(YOUR_PACKAGE_NAME_HERE): $(YOUR_OBJECTS_HERE)
     g++ $(SOME_LDFLAGS_HERE) $^ -o $@

clean:
     rm $(YOUR_PACKAGE_NAME_HERE) $(YOUR_OBJECTS_HERE)

.PHONY: clean

There are good tutorials outthere, like this one. 有很好的教程,像这样的

If you rename your cpp file to sim.cpp this is probably the simplest Makefile 如果将cpp文件重命名为sim.cpp这可能是最简单的Makefile

CPPFLAGS := -I. -I$(SYSTEMC_HOME)/include
LDFLAGS  := -L. -L$(SYSTEMC_HOME)/lib-linux64
LDLIBS   := -lsystemc -lm

sim:

.PHONY: clean
clean: ; $(RM) sim

The implicit rules should handle everything else for you 隐式规则应该为您处理其他一切

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

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