c - Unable to pass additional flags to gcc through mpicc
问题描述
I have a C program that uses OpenMPI libraries. I am also using functions from libm and have included math.h
in my program. When compiling using mpicc
, like so
mpicc -lm program.c -o program.out
compilation succeeds, but linking fails, with ld
unable to link libm and the following three lines of error
/usr/bin/ld: /tmp/cct0O5Yv.o: undefined reference to symbol 'log10@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
From what I understand, mpicc
is just a wrapper for gcc
whose job is to pass compilation and linking flags to gcc
. Why is it not passing -lm
to gcc
? How do I make it pass additional flags to gcc
? I couldn't fine anything in the man pages.
I used --showme:compiler
and --showme:linker
with mpicc
and passed those flags to gcc
along with -lm
, and it generates the final executable just fine.
解决方案
It turns out that the order of the command line arguments passed to mpicc
matters. In order to make mpicc
pass additional flags to gcc
, place them before the source file
mpicc -lm program.c -o program.out
Flags placed after the source file will be treated as flags to the wrapper and not the "kernel" (in this case gcc
).