Default linker setting in Makefile for linking C++ object files

后端 未结 1 1486
情书的邮戳
情书的邮戳 2021-02-05 04:08

Consider this Makefile

% cat Makefile
main: main.o add.o

which uses cc instead of g++ to link the object

相关标签:
1条回答
  • 2021-02-05 04:46

    (GNU) Make has built-in rules, which is nice, because it is enough to give dependencies without rules:

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

    However the build-in rule in this case uses $(CC) for linking object files.

    % 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 $@
    

    To let Make chose the correct linker, it is sufficient to set LINK.o to LINK.cc. A minimal Makefile can therefore look like

    % cat Makefile
    LINK.o = $(LINK.cc)
    CXXFLAGS=-Wall -pedantic -std=c++0x
    
    main: main.o add.o
    
    0 讨论(0)
提交回复
热议问题