CMake的简单使用
系统移植后,原来的应用程序在新的环境下跑,报Segmentation fault段错误,刚开始怀疑是程序的原因,这就需要对应用程序代码进行调试(最后确认是移植过程中使用不同的编译器和库引起的),应用程序是人家在KDevelop下开发的,我虚拟机上的Ubuntu没装Kdvelop(且不能联外网),于是就得自己动手写Makefile。
虽然看过蛮多的Makefile文件,但真正自己动手写还是有一定难度,且根本没那么多时间给你慢慢学习Makefile,于是就找了一个Cmake,它会根据配置自动生成makefile,这样就轻松多了。
网下载安装包并安装,如果你linux系统可以联网的话,sudo apt-get install cmake(ubuntu下)或者sudo yum install cmake(fedora下): http://www.cmake.org/cmake/resources/software.html
安装好后可用cmake --help确认有没安装好。[lanux@vmware:work]-14:55$ cmake --helpcmake version 2.8.3Usage cmake [options] <path-to-source> cmake [options] <path-to-existing-build>
CMake会根据CMakeLists.txt中的配置来生成Makefile文件,注意这里有一种方法可以指定自己的交叉编译器(尽管应避免用此种方法),官网FAQ http://www.cmake.org/Wiki/CMake_FAQ上原文第三种方法:Method 3 (avoid): use set() Set the appropriate CMAKE_FOO_COMPILER variable(s) to a valid compiler name or full path in a list file using set(). This must be done before any language is set (ie before any project() or enable_language() command). For example: set(CMAKE_C_COMPILER "gcc-4.2")set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.2")project("YourProjectName")
在实践中,发现set(CMAKE_C_COMPILER "arm-s3c2416-linux-gnueabi-gcc")等指定交叉编译此部分必须放在project指令前面,否则cmake还会自动去找自带的gcc(还不知是什么原因)。一本很好的介绍CMake的书籍:http://sewm.pku.edu.cn/src/paradise/reference/CMake Practice.pdf
参考资料:1、Cmake官网的CMake FAQ http://www.cmake.org/Wiki/CMake_FAQ2、在 linux 下使用 CMake 构建应用程序http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/
附:(工程比较小,且在同一个目录下)。
[lanux@vmware:passthru_self]-15:07$ lsCMakeLists.txt include.h InterfaceBase.h InterfaceUsb.hcrc.cpp IntComm.cpp InterfaceNet.cpp main.cppHostPortInterface.cpp IntComm.h InterfaceNet.h MessageLoop.cppHostPortInterface.h InterfaceBase.cpp InterfaceUsb.cpp MessageLoop.h [lanux@vmware:passthru_self]-15:07$ cmake .-- The C compiler identification is GNU-- The CXX compiler identification is GNU-- Check for working C compiler: /usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-gcc………………-- Configuring done-- Generating done-- Build files have been written to: /home/lanux/work/passthru_self[lanux@vmware:passthru_self]-15:09$ makeCMake Warning (dev) in CMakeLists.txt:………………[ 12%] Building CXX object CMakeFiles/passthru.dir/HostPortInterface.o[ 25%] Building CXX object CMakeFiles/passthru.dir/InterfaceUsb.o………………[ 87%] Building CXX object CMakeFiles/passthru.dir/InterfaceBase.o[100%] Building CXX object CMakeFiles/passthru.dir/main.oLinking CXX executable passthru[100%] Built target passthru[lanux@vmware:passthru_self]-15:09$cat CMakeLists.txt
#指定交叉编译,注意此部分必须放在project前面,否则cmake还会自动去找自带的gcc(还不知是什么原因),但这种方法官网是不推荐的。set(CMAKE_C_COMPILER "/usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-gcc")set(CMAKE_CXX_COMPILER "/usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-g++")set(CMAKE_AR "/usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-ar")set(CMAKE_LD "/usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-ld")set(CMAKE_NM "/usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-nm")set(CMAKE_STRIP "/usr/local/arm/linux_arm_2416eabi/bin/arm-s3c2416-linux-gnueabi-strip")PROJECT(passthru)
#指定依赖关系ADD_EXECUTABLE(passthru HostPortInterface.cpp InterfaceUsb.cpp InterfaceNet.cpp IntComm.cpp crc.cpp MessageLoop.cpp InterfaceBase.cpp main.cpp)
#指定库路径LINK_DIRECTORIES("/usr/local/arm/linux_arm_2416eabi/lib")#TARGET_LINK_LIBRARIES(passthru libpthread-2.12.1.so)TARGET_LINK_LIBRARIES(passthru libpthread.so)[lanux@vmware:passthru_self]-15:30$