Simple use of cmake

Source: Internet
Author: User

Simple use of cmake
 

After the system is transplanted, the original application runs in the new environment and reports a segmentation fault segment error. At first, it is suspected that it is the cause of the program, this requires debugging of the application code (the final confirmation is caused by the use of different compilers and libraries during the porting process). The application is developed under kdevelop, kdvelop is not installed on Ubuntu on my VM (and cannot be connected to the Internet), so you have to write makefile by yourself.

Although I have read a lot of makefile files, it is still difficult to write them by myself. I didn't have much time to learn makefile for you, so I found a cmake, it automatically generates makefile according to the configuration, which makes it much easier.

Download the installation package and install, if your Linux system can be networked, sudo apt-Get install cmake (under Ubuntu) or sudo Yum install cmake (under fedora): http://www.cmake.org/cmake/resources/software.html

 
After installation, use cmake -- help to check whether the installation is successful.
[Lanux @ VMware: Work]-$ cmake -- Help
Cmake version 2.8.3
Usage
Cmake [Options] <path-to-source>
Cmake [Options] <path-to-existing-build>

Cmakewill generate the MAKEFILE file through the configuration in cmakelists.txt. Note that there is a way to specify your own cross-compiler (although this method should be avoided). On the official website FAQ http://www.cmake.org/wiki/cmake_faq, the third type is:
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 ")

In practice, it is found that set (cmake_c_compiler "arm-s3c2416-linux-gnueabi-gcc") and other specified cross-compiling this part must be placed in front of the Project command, otherwise, cmake will automatically find the built-in GCC (for some reason ). A good book on cmake: http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf

References:
1. cmake official website cmake FAQ http://www.cmake.org/Wiki/CMake_FAQ
2. Using cmake to build an application http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ in Linux

 

 

Appendix: (the project is small and in the same directory ).

[Lanux @ VMware: passthru_self]-15:07 $ ls
Cmakelists.txt include. h interfacebase. h interfaceusb. hcrc. cpp intcomm. cpp interfacenet. cpp main. cpp
Hostportinterface. cpp intcomm. h interfacenet. h messageloop. cpp
Hostportinterface. h interfacebase. cpp interfaceusb. cpp messageloop. h
[Lanux @ VMware: passthru_self]-$ 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
..................
-- Processing ing done
-- Generating done
-- Build files have been written to:/home/lanux/work/passthru_self
[Lanux @ VMware: passthru_self]-15:09 $ make
Cmake warning (Dev) in cmakelists.txt:
..................
[2, 12%] building cxx object cmakefiles/Passthru. DIR/hostportinterface. o
[2, 25%] building cxx object cmakefiles/Passthru. DIR/interfaceusb. o
..................
[2, 87%] building cxx object cmakefiles/Passthru. DIR/interfacebase. o
[100%] building cxx object cmakefiles/Passthru. DIR/Main. o
Linking cxx executable passthru
[1, 100%] built target passthru
[Lanux @ VMware: passthru_self]-15:09 $ cat cmakelists.txt

 

# Specify cross-compilation. Note that this part must be placed before the project. Otherwise, cmake will automatically find the built-in GCC (for some reason). However, this method is not recommended on the official website.
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)

# Specify dependency
Add_executable (passthru hostportinterface. cpp
Interfaceusb. cpp
Interfacenet. cpp
Intcomm. cpp
CRC. cpp
Messageloop. cpp
Interfacebase. cpp
Main. cpp)

# Specify the library path
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 $

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.