【问题标题】:c++ how should the makefile lookc ++ makefile应该如何看
【发布时间】:2014-05-06 09:03:27
【问题描述】:

假设我有以下这些 c++ 文件,我应该如何为它们编写基本的 makefile(使用 g++)?
a.cpp a.h, b.cpp b.h, c.cpp c.h, main.h
当b.h包含a.h时,c.h包含b.h,main.h包含c.h?

非常感谢

【问题讨论】:

    标签: makefile


    【解决方案1】:

    这样你就可以写了。

    EXE := exe
    CC := g++
    CPP_FILES := a.cpp b.cpp c.cpp
    HPP_FILES := a.h b.h c.h main.h
    
    $(EXE) : $(CPP_FILES) $(HPP_FILES)
          $(CC) -o $@ $(CPP_FILES)
    .PHONY : clean
    clean:
          rm -rf $(EXE) *.o
    

    【讨论】:

      【解决方案2】:

      如何编译文件?假设你现在有test.cpptest.h,来编译和链接它:

      g++ -c test.c
      g++ -o test test.o
      

      最简单的Makefile:

      test: test.o                #means test depends on test.o
              g++ -o test test.o
      test.o: test.cpp test.h     #means test.o depends on test.cpp and test.h
              g++ -c test.cpp
                                  #if you want clean? add below line too.
      clean:
          rm test test.o
      

      如果你的应用依赖多个文件,那么

      test: test1.o test2.o test3.o #means your app depends on test1.o test2.o and test3.o
              g++ -o test test1.o test2.o test3.o
      test1.o: test1.cpp test1.h
              g++ -c test1.cpp
      test2.o: test2.cpp test2.h
              g++ -c test2.cpp
      ...
      

      【讨论】:

      • 如果我也有包含test.h的test2.h,test2.cpp,应该怎么写呢?
      猜你喜欢
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多