How To Write CmakeLists.Txt

How To Write CmakeLists.Txt

If u have ever run into problems of porting a Visual C++ (Visual Studio) project/ solution from one location to other or even worse if from one machine to other with an entirely different version of Visual C++ / Visual Studio as well as different configurations of add on libraries, then this post is for you.

We present a very easy and simple way of porting one or all files of a Visual C++ project from one location/ machine to any other place, even in fact across platforms like Windows, Linux etc.

For the said purpose we will make use of CMake utility i.e. the cross platform make utility. Its a freely available utility and can be downloaded from here. This utility requires only all your source files i.e. .c /.cpp files and the header files (.h) files in addition to one more file i.e. CMakeLists.Txt.

The main target of writing this post is to familiarise with the procedure of writing a simple CmakeLists.Txt file, which is very simple to write. A very complex documentation which is available online as on date, makes it very difficult for a novice to make use of this very beneficial tool. So here we go, writing a very simple CmakeLists.Txt and explaining it each statement side by side. We are taking a very complex project having multiple header files, with numerous external libraries and additional dependencies, so as to completely explain the concept.

Following is a completely working version of CMakeLists.Txt which you may use as required by you after modifications. Please note that all statement beginning with # are taken as comments by CMake and will be used for explaination of the file to you.

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
# Above line indicates to Cmake that minimum version 2.8 is required.
# As you may have noted all lines beginning with hash symbol are treated as comments by Cmake.
SET(sampleName MyApp)
# In the above line we declared a variable sampleName and assigned it a value MyApp. We will make use of the same later.project(PrjMyApp)
# here we define the project or solution (for visual studio) name

# In following lines we try to find out the packages of additional libraries, if reqd. Here we are trying to locate PCL and mrpt library.
# u may replace the names by yours. Each such package should have a Cmake file in them, if not then we may have to manually define the paths.
# we Show that later.
find_package(PCL 1.2 REQUIRED)
FIND_PACKAGE(MRPT REQUIRED base;bayes;obs;gui)

# here we specify the additional include directories for the project. These files come in additional include directories option of VC++
# project.
# either the variable values from package like ${PCL_INCLUDE_DIRS} or absolute paths as shown in second and third line may be used.
include_directories(${PCL_INCLUDE_DIRS})
include_directories("G:/Matlab/extern/include")
include_directories("C:/Program Files/MobileRobots/Aria/include")

# here we specify the additional library directories for the linker in the project. These files come in additional library directories
# option of VC++ project.
# either the variable values from package like ${PCL_LIBRARY_DIRS} or absolute paths as shown in second and third line may be used.
# An update, link_directories is being pulled out, you may use TARGET_LINK_LIBRARIES instead as shown below
link_directories(${PCL_LIBRARY_DIRS})
link_directories("G:/Matlab/extern/lib/win32/microsoft")
link_directories("C:/Program Files/MobileRobots/Aria/lib")

# here we add definitions for any package if requred.
add_definitions(${PCL_DEFINITIONS})

# The following line is very important.
# It specifies the executable name. Here the executable is the first parameter i.e. MyApp and a file MyApp.exe will be created on
# compilation in windows platform.
# Followed by the executable name come all your source and header files.
# All cpp fles will be clubbed into source folder and all .h files will be added to header files folder of the project.
add_executable (MyApp Local.cpp part.h grab.h interface.h test.cpp test.h)

# There may be some additional dependencies which you may have to specify for the project, which you may do as in following lines.
# Note that first parameter is the executable name.
target_link_libraries (MyApp ${PCL_LIBRARIES} libeng.lib libmx.lib libmex.lib libmat.lib Aria.lib winmm.lib wsock32.lib)
# Add the required libraries for linking:
TARGET_LINK_LIBRARIES(${sampleName}
${MRPT_LIBS} # This is filled by FIND_PACKAGE(MRPT ...)
"" # Optional extra libs...
)
# End of CMakeLists.Txt file. Save it as a plain text file and the name should be exactly CMakeLists.Txt

After you have created your CMakeLists.Txt file, save it in the same folder as all your *.c/*.cpp/*.h files.

Create a new folder to hold all your project/ solution files within same folder. Lets say u name it MyPrjFiles.

Now open the CMake Gui application (These are for Windows platform only, for Linux refer to CMake instructions here).

Specify the source folder location as the location where you have kept your source, header and CMakeLists.Txt file.

Specify the location of MyPrjFiles folder in the text field for where to build binaries.

How to write and use CMakeLists.Txt

Then click on the configure button on the bottom. It will read your CMakeLists.Txt file and try to configure the project for you. It is in fact checking for errors (if any) and will ask you for the version of compiler for which binaries are to be built for. Specify the correct version of your compiler. In case of errors it will report in red. You will have to rectify the errors before proceeding further.

After no errors are reported, you may click on generate button. And ur job is done. You will find all your reqired solution and project files in MyPrjFiles folder. When you open your solution and try to compile it, it compiles so smoothly i.e. there are no problems of setting up the environment.

Now for taking a backup or shifting your project from one place to other, just take all your source file and ur CMakeLists.Txt file to new location and generate project in new environment once using CMake utility. So you have the complete independence now to work between multiple machines, multiple environments, multiple platforms without a hassle. Just keep a CMakeLists.Txt file for each place handy and it does the work for you.

Please do drop in your feedback/ suggestions for the article below……

原文地址: http://preetisblog.com/programming/how-to-write-cmakelists-txt


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值