【问题标题】:Default linker setting in Makefile for linking C++ object filesMakefile 中用于链接 C++ 目标文件的默认链接器设置
【发布时间】:2012-11-14 08:36:18
【问题描述】:

考虑一下Makefile

% cat Makefile
main: main.o add.o

它使用cc 而不是g++ 来链接目标文件

% make
g++ -Wall -pedantic -std=c++0x   -c -o main.o main.cpp
g++ -Wall -pedantic -std=c++0x   -c -o add.o add.cpp
cc   main.o add.o   -o main
main.o:main.cpp:(.text+0x40): undefined reference to `std::cout'
...

我如何告诉 (GNU) Make 使用 g++(链接 C++ 库)而不是 cc

【问题讨论】:

    标签: makefile


    【解决方案1】:

    (GNU) Make 有内置的规则,这很好,因为它足以提供没有规则的依赖:

    main: main.o add.o
        # no rule, therefore use built-in rule
    

    但是在这种情况下,内置规则使用$(CC) 链接目标文件。

    % make -p -f/dev/null
    ...
    LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)
    ...
    LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
    ...
    %: %.o
    #  recipe to execute (built-in):
            $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
    

    要让 Make 选择正确的链接器,将 LINK.o 设置为 LINK.cc 就足够了。因此,最小的Makefile 看起来像

    % cat Makefile
    LINK.o = $(LINK.cc)
    CXXFLAGS=-Wall -pedantic -std=c++0x
    
    main: main.o add.o
    

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多