一. 前言

在编译开源软件的时候,有时会遇到”configure: error: C compiler cannot create executables”的错误,表示不能生成可执行文件。本文以编译curl-7.40.0为例,模拟出这种错误,并讲解解决这种错误的方法。错误输出如下:

[root@192 curl-7.40.0]# ./configure LIBS=-lopenssl
checking whether to enable maintainer-specific portions of Makefiles… no
checking whether to enable debug build options… no
checking whether to enable compiler optimizer… (assumed) yes
checking whether to enable strict compiler warnings… no
checking whether to enable compiler warnings as errors… no
checking whether to enable curl debug memory tracking… no
checking whether to enable hiding of library internal symbols… yes
checking whether to enable c-ares for DNS lookups… no
checking for path separator… :
checking for sed… /usr/bin/sed
checking for grep… /usr/bin/grep
checking for egrep… /usr/bin/grep -E
checking for ar… /usr/bin/ar
checking for a BSD-compatible install… /usr/bin/install -c
checking for gcc… gcc
checking whether the C compiler works… no
configure: error: in `/home/betterman/opensource/curl-7.40.0′:
configure: error: C compiler cannot create executables
See `config.log’ for more details

二. 错误解析

根据最后一句输出,我们需要参考config.log的内容。从config.log文件中,可以找到如下一段内容:

configure:4216: checking whether the C compiler works
configure:4238: gccconftest.c -lopenssl >&5 (1)
/usr/bin/ld: cannot find -lopenssl (2)
collect2: error: ld returned 1 exit status
configure:4242: $? = 1
configure:4280: result: no
configure: failed program was:
| /* confdefs.h */(3)
| #define PACKAGE_NAME “curl”
| #define PACKAGE_TARNAME “curl”
| #define PACKAGE_VERSION “-“
| #define PACKAGE_STRING “curl -“
| #define PACKAGE_BUGREPORT “a suitable curl mailing list: http://curl.haxx.se/mail/”
| #define PACKAGE_URL “”
| /* end confdefs.h. */
|
| int main (void)
| {
|
| ;
| return 0;
| }
configure:4285: error: in `/home/wenyong/opensource/curl-7.40.0′:
configure:4287: error: C compiler cannot create executables(4)
See `config.log’ for more details

从(1)可知,gcc会编译conftest.c文件并且需要链接libopenssl.so,所以报(2)的错误,表示编译时找不到libopenssl.so库,conftest.c的内容是从(3)开始,最后产生了(4)处的错误。

三. 解决方法

由上可知,可以得出结论:软件包在configure的时候,configure会根据用户指定的编译选项编译conftest.c文件,由此来预检查编译时要用到的gcc,动态库等是否可以找到,尽早让用户检查错误。

本例的错误是由于libopenssl.so找不到,所以解决方法有:

1. 通过LDFLAGS选项指定libopenssl.so所在的路径,例如:

./configure LIBS=-lopensslLDFLAGS=/root/libopenssl/usr/lib

2. 直接使用./configure编译

如果编译curl不需要用到openssl的库,可以不链接。

四. 总结

本文讲解了解决”configure: error: C compiler cannot create executables”问题出现的原因与解决的思路:configure运行的时候需要使用用户指定的选项编译conftest.c文件,但是由于选项指定的gcc或动态库找不到,导致编译报错,解决的方法是指定gcc和动态库的路径。