上半年时做了一个将Linux移植到Windows的项目,由于对configure&make这个过程不太熟悉,所以还是出了很多问题的。
现在简单整理当时的笔记。
Q1:
#ifdef x
...
#else
...
#endif
这些东西是干什么的?
A1:一些程序在设计时,希望能对环境进行一些适应从而编译出不同的软件,比如在不同的操作系统下都能编译。我们可以通过变量设置,把所有可能用到的代码都写进去,在初始化时配置,但在不同的情况下可能只用到一部分代码,就没必要把所有的代码都写进去,就可以用条件编译,通过预编译指令设置编译条件,在不同的需要时编译不同的代码。比如
#ifdef OS_WINDOWS
os.name = "windows";
#else
os.name = "other"
#endif
Q2:这些条件编译使用的变量怎么来的?为什么在config.h(类似)里看到了这些值?
A2:既然是为适应环境而产生的条件编译,那这些变量当然不能是写死的,一般来说Linux下的configure过程会分析这些变量该取哪些值的过程,然后会将这值写进一个头文件里,并让其他文件包含(config.h),生成Makefile文件
某工程的config.h如下
/* config.h. Generated from config.h.in by configure. */
/* config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
/* Define to 1 if you have the `dnet' library (-ldnet). */
#define HAVE_LIBDNET 1
/* Define to 1 if you have the `dumbnet' library (-ldumbnet). */
/* #undef HAVE_LIBDUMBNET */
/* Define to 1 if you have the `pcap' library (-lpcap). */
/* #undef HAVE_LIBPCAP */
/* Define to 1 if you have the `pthread' library (-lpthread). */
/* #undef HAVE_LIBPTHREAD */
/* Define to 1 if you have the `wpcap' library (-lwpcap). */
#define HAVE_LIBWPCAP 1
/* Define to 1 if you have the `ws2_32' library (-lws2_32). */
#define HAVE_LIBWS2_32 1
/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
/* Defined when the system UNIX based */
/* #undef OS_UNIX */
/* Defined when the system is Windows based */
#define OS_WINDOWS
/* Name of package */
Q3::configure时提示 can not guess build type怎么办?
A3:这时就是configure脚本无法判断当前的一些信息,比如操作系统,这时需要我们去指定,比如
--target=GARGET
除此之外,这个问题的产生是由于config.guess脚本无法guess产生的,一般都是因为config.guess比较老导致的,我们更新脚本即可。
地址:http://git.savannah.gnu.org/gitweb/?p=config.git
替换config.guess
重新configure
OK