Makefiile有很多旗帜

时间:2015-06-12 18:31:37

标签: c++ linux makefile linker intel-mkl

我正在尝试学习Makefile应该是什么样子,当谈到标志时,尤其是链接标志。这是我的Makefile:

OBJS    = n.o
SOURCE  = n.cpp
# HEADER = there are no header files, so I commented that
OUT     =       test
CXX     = ../mpich-install/bin/mpic++
FLAGS   =       -I../intel/mkl/include  ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a       -Wl,--start-group       ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a ../intel/mkl/lib/intel64/libmkl_core.a  ../intel/mkl/lib/intel64/libmkl_sequential.a    -Wl,--end-group ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a -lpthread       -lm     -ldl

all: $(OBJS)
        $(CXX)  $(OBJS) -o $(OUT)       $(FLAGS)

# create/compile the individual files >>separately<<
n.o:    n.cpp
        $(CXX)  -c      n.cpp   $(FLAGS)

.PHONY : all
# clean house
clean:
        rm -f $(OBJS)

然后我得到了:

../mpich-install/bin/mpic++     -c      n.cpp   -I../intel/mkl/include  ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a       -Wl,--start-group       ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a   ../intel/mkl/lib/intel64/libmkl_core.a  ../intel/mkl/lib/intel64/libmkl_sequential.a    -Wl,--end-group ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a  -lpthread       -lm     -ldl
g++: warning: ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_core.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_sequential.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a: linker input file unused because linking not done
../mpich-install/bin/mpic++     n.o -o test     -I../intel/mkl/include  ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a       -Wl,--start-group       ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a   ../intel/mkl/lib/intel64/libmkl_core.a  ../intel/mkl/lib/intel64/libmkl_sequential.a    -Wl,--end-group ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a  -lpthread       -lm     -ldl

这意味着我应该只在进程的最后部分使用一些标志。处理这种情况的正确方法是什么?也许创建FLAGS1FLAGS2?它应该工作,但我想知道哪种方法正确。

1 个答案:

答案 0 :(得分:4)

  

&#34; ...但我想知道哪种方法正确。&#34;

正确的方法是跟上make标准变量名称,特别是CXXFLAGSLDFLAGS

您不希望为此处尝试指定链接器标志的库(实际指定主题):

FLAGS   =  ... ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a

您应该使用标准FLAGS makefile变量来设置路径,而不是使用LDFLAGS并直接提供链接主题,并让链接器找到适当的静态或共享库: / p>

  LDFLAGS += -L../intel/mkl/lib/intel64 -lmkl_scalapack_ilp64
# ^^^^^^^ Note the standard build system uses $(LDFLAGS) at the linker stage rule
# -L specifies the paths for finding libraries
# -l<MyLib> actually searches for certain libraries in the given paths,
#           expanding to search for files like libMyLib.a or libMyLib.so.

all: $(OBJS)
        $(CXX)  $(OBJS) -o $(OUT) $(CXXFLAGS) $(LDFLAGS)
#                                          ^^^^^^^^^^ Use them separately at
#                                                     linking stage  
        make    -f      Makefile        clean
#       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This whole line looks very (!!!) suspicious,
#                                             and would just make it harder to debug your
#                                             build system. It's an indicator, you did
#                                             dependencies management wrong.
#                                             Consider to use the compiler's --MF options 
#                                             family, to create a dependency file for the 
#                                             headers, and include it in your makefile.

区别于编译阶段标志(CXXFLAGS)和链接阶段标志(LDFLAGS)。

除了:

要避免这种情况

make    -f      Makefile        clean

最终目标规则中的操作(可能已添加以避免错过以赶上标头依赖关系),向-MF添加CXXFLAGS选项,并包含结果。

以下是有关各种技术的更详细信息:

<强> Auto-Dependency Generation