gcc -I/opt/cplex-studio-125/cplex/include -lcplex -L/opt/cplex-studio-125/cplex/lib/x86-64_sles10_4.1/static_pic mipex1.c
我试图从cplex编译一个例子。 -I
选项没问题。 -L
选项似乎已损坏,因为它无法找到函数名称。
错误在哪里?在特定链接中有一个libcplex.a
。
/tmp/ccf5sKky.o: In function `main':
mipex1.c:(.text+0x9f): undefined reference to `CPXopenCPLEX'
mipex1.c:(.text+0xee): undefined reference to `CPXgeterrorstring'
mipex1.c:(.text+0x125): undefined reference to `CPXsetintparam'
mipex1.c:(.text+0x222): undefined reference to `CPXcreateprob'
mipex1.c:(.text+0x2e1): undefined reference to `CPXcopylp'
mipex1.c:(.text+0x334): undefined reference to `CPXcopyctype'
mipex1.c:(.text+0x380): undefined reference to `CPXmipopt'
mipex1.c:(.text+0x3cc): undefined reference to `CPXgetstat'
mipex1.c:(.text+0x403): undefined reference to `CPXgetobjval'
mipex1.c:(.text+0x474): undefined reference to `CPXgetnumrows'
mipex1.c:(.text+0x490): undefined reference to `CPXgetnumcols'
mipex1.c:(.text+0x4be): undefined reference to `CPXgetx'
mipex1.c:(.text+0x51c): undefined reference to `CPXgetslack'
mipex1.c:(.text+0x5f9): undefined reference to `CPXwriteprob'
mipex1.c:(.text+0x64d): undefined reference to `CPXfreeprob'
mipex1.c:(.text+0x697): undefined reference to `CPXcloseCPLEX'
mipex1.c:(.text+0x6e3): undefined reference to `CPXgeterrorstring'
collect2: error: ld returned 1 exit status
答案 0 :(得分:4)
order of appearance of the library matters。将编译语句更改为
gcc -I/opt/cplex-studio-125/cplex/include mipex1.c L/opt/cplex-studio-125/cplex/lib/x86-64_sles10_4.1/static_pic -lcplex
(通用版)
gcc -I/<path> -L<path> <source.c> -l<lib>
将库放在源文件之后,以便链接器在库中搜索从源文件调用的函数。