当前位置:首页 » 编程软件 » cmakelist的编译

cmakelist的编译

发布时间: 2022-07-02 10:36:28

❶ windows怎么使用 cmakelist编译

CMake是一个比make更高级的编译配置工具,它可以根据不同平台、不同的编译器,生成相应的Makefile或者vcproj项目。 通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程。

linux cmakelist 怎么用

由于调试需要因此研究了一下cmake这个夸平台的编译工具的使用方法.

1.本人的机器为ubuntu 10.04,在连网的情况下直接在终端输入:

[cpp] view plain 在CODE上查看代码片派生到我的代码片
root@zsh-linux:~#apt-get install cmake

安装完毕之后可以在/var/cache/apt/archives看到安装的.deb文件

或者在cmake官网下载cmake for linux

此时有个注意点是建议下载 cmake-2.8.4.tar.gz 而不是

cmake-2.8.4-Linux-i386.tar.gz

下载完成后解压

[cpp] view plain 在CODE上查看代码片派生到我的代码片
root@zsh-linux:/opt#tar -zxvf cmake-2.8.4.tar.gz
然后 cd 到cmake-2.8.4目录下
root@zsh-linux:/opt/cmake-2.8.4#
root@zsh-linux:/opt/cmake-2.8.4# ./bootstrap
root@zsh-linux:/opt/cmake-2.8.4# make
root@zsh-linux:/opt/cmake-2.8.4# make install
安装完毕后查看是否安装成功:
root@zsh-linux:/opt/cmake-2.8.4# cmake --version
cmake version 2.8.4
有以上信息表示安装cmake成功。

2.cmake 的使用

(1)创建一个工程目录文件夹,然后创建一个hello.c

[cpp] view plain 在CODE上查看代码片派生到我的代码片
#include<stdio.h>
int main()
{
printf(“hello,this is my first using cmake project/n”);
return 0;
}
(2)然后创建一个build目录(用于编译生成的相应文件),与hello.c目录同级

(3)编写CMakeLists.txt内容如下:(于hello.c目录同级)

[cpp] view plain 在CODE上查看代码片派生到我的代码片
cmake_minimum_required(VERSION 2.8)
PROJECT(cmake_test)
SET(SRC_LIST main.c)
INCLUDE_DIRECTORIES(/usr/include/glib)
MESSAGE(STATUS "This is BINARY dir "${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir "${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})
(4)进入build目录输入 cmake ..

[cpp] view plain 在CODE上查看代码片派生到我的代码片
root@zsh-linux:/home/cmake_test/build# cmake ..
若编译成功在build目录下会生成相应文件,其中有个makefile文件

有可能会出现问题:

CMAKE_CXX_COMPILER-NOTFOUND" was not found

解决方法:

[cpp] view plain 在CODE上查看代码片派生到我的代码片
root@zsh-linux:/home/cmake_test/build# apt-get install g++
(可选)cmake -D CMAKE_CXX_COMPLIER=”g++”CMAKE -D CMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=”/usr/local”
(5)输入make 命令执行成功后在build目录下会看到可执行的hello

(6)./hello

输出 hello,this is my first usingcmake project。

注:这只适用于简单的工程,若复杂的工程项目请参考www.cmake.org

❸ 怎么配置cmakelist交叉编译

cmake交叉编译配置

很多时候,我们在开发的时候是面对嵌入式平台,因此由于资源的限制需要用到相关的交叉编译。即在你host宿主机上要生成target目标机的程序。里面牵扯到相关头文件的切换和编译器的选择以及环境变量的改变等,我今天仅仅简单介绍下相关CMake在面对交叉编译的时候,需要做的一些准备工作。

CMake给交叉编译预留了一个很好的变量CMAKE_TOOLCHAIN_FILE,它定义了一个文件的路径,这个文件即toolChain,里面set了一系列你需要改变的变量和属性,包括C_COMPILER,CXX_COMPILER,如果用Qt的话需要更改QT_QMAKE_EXECUTABLE以及如果用BOOST的话需要更改的BOOST_ROOT(具体查看相关Findxxx.cmake里面指定的路径)。CMake为了不让用户每次交叉编译都要重新输入这些命令,因此它带来toolChain机制,简而言之就是一个cmake脚本,内嵌了你需要改变以及需要set的所有交叉环境的设置。

toolChain脚本中设置的几个重要变量

1.CMAKE_SYSTEM_NAME:

即你目标机target所在的操作系统名称,比如ARM或者Linux你就需要写"Linux",如果Windows平台你就写"Windows",如果你的嵌入式平台没有相关OS你即需要写成"Generic",只有当CMAKE_SYSTEM_NAME这个变量被设置了,CMake才认为此时正在交叉编译,它会额外设置一个变量CMAKE_CROSSCOMPILING为TRUE.

2. CMAKE_C_COMPILER:

顾名思义,即C语言编译器,这里可以将变量设置成完整路径或者文件名,设置成完整路径有一个好处就是CMake会去这个路径下去寻找编译相关的其他工具比如linker,binutils等,如果你写的文件名带有arm-elf等等前缀,CMake会识别到并且去寻找相关的交叉编译器。

3. CMAKE_CXX_COMPILER:

同上,此时代表的是C++编译器。

4. CMAKE_FIND_ROOT_PATH:

指定了一个或者多个优先于其他搜索路径的搜索路径。比如你设置了/opt/arm/,所有的Find_xxx.cmake都会优先根据这个路径下的/usr/lib,/lib等进行查找,然后才会去你自己的/usr/lib和/lib进行查找,如果你有一些库是不被包含在/opt/arm里面的,你也可以显示指定多个值给CMAKE_FIND_ROOT_PATH,比如

set(CMAKE_FIND_ROOT_PATH /opt/arm /opt/inst)

该变量能够有效地重新定位在给定位置下进行搜索的根路径。该变量默认为空。当使用交叉编译时,该变量十分有用:用该变量指向目标环境的根目录,然后CMake将会在那里查找。

5. CMAKE_FIND_ROOT_PATH_MODE_PROGRAM:

对FIND_PROGRAM()起作用,有三种取值,NEVER,ONLY,BOTH,第一个表示不在你CMAKE_FIND_ROOT_PATH下进行查找,第二个表示只在这个路径下查找,第三个表示先查找这个路径,再查找全局路径,对于这个变量来说,一般都是调用宿主机的程序,所以一般都设置成NEVER

6. CMAKE_FIND_ROOT_PATH_MODE_LIBRARY:

对FIND_LIBRARY()起作用,表示在链接的时候的库的相关选项,因此这里需要设置成ONLY来保证我们的库是在交叉环境中找的.

7. CMAKE_FIND_ROOT_PATH_MODE_INCLUDE:

对FIND_PATH()和FIND_FILE()起作用,一般来说也是ONLY,如果你想改变,一般也是在相关的FIND命令中增加option来改变局部设置,有NO_CMAKE_FIND_ROOT_PATH,ONLY_CMAKE_FIND_ROOT_PATH,BOTH_CMAKE_FIND_ROOT_PATH

8. BOOST_ROOT:

对于需要boost库的用户来说,相关的boost库路径配置也需要设置,因此这里的路径即ARM下的boost路径,里面有include和lib。

9. QT_QMAKE_EXECUTABLE:

对于Qt用户来说,需要更改相关的qmake命令切换成嵌入式版本,因此这里需要指定成相应的qmake路径(指定到qmake本身)

toolChain demo

# this is required
SET(CMAKE_SYSTEM_NAME Linux)

# specify the cross compiler
SET(CMAKE_C_COMPILER /opt/arm/usr/bin/ppc_74xx-gcc)
SET(CMAKE_CXX_COMPILER /opt/arm/usr/bin/ppc_74xx-g++)

# where is the target environment
SET(CMAKE_FIND_ROOT_PATH /opt/arm/ppc_74xx /home/rickk/arm_inst)

# search for programs in the build host directories (not necessary)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# configure Boost and Qt
SET(QT_QMAKE_EXECUTABLE /opt/qt-embedded/qmake)
SET(BOOST_ROOT /opt/boost_arm)

这样就完成了相关toolChain的编写,之后,你可以灵活的选择到底采用宿主机版本还是开发机版本,之间的区别仅仅是一条-DCMAKE_TOOLCHAIN_FILE=./toolChain.cmake,更爽的是,如果你有很多程序需要做转移,但目标平台是同一个,你仅仅需要写一份toolChain放在一个地方,就可以给所有工程使用。

❹ cmakelists.txt 怎么编译

./configure就是执行你当前目录下一个名叫configure的脚本,由它生成Makefile,有了Makefile之后,一般来说就可以通过make进行编译,make install进行安装 cmake就是一个与make同级别的编译工具,只不过它依靠的不是Makefile作为编译规则

❺ 怎样编写Cmake的配置文件Cmakelist.txt

  1. Cmake 有linux ,windows 多个平台版本,如图是windows下版本程序

    怎样编写Cmake的配置文件Cmakelist.txt

  2. 更具一个简单多目录c项目,学下cmakelist.txt编写规范

  3. 根目录下cmakelist文件内容:
    set(CMAKE_INSTALL_PREFIX):设置程序的安装目录,优先级比cmake命令参数设置高。
    add_subdirectory(编译文件子目录)

  4. libhello 目录下的cmakelist文件内容:

  5. libhello 目录下的cmakelist文件内容:
    查看编译后结果:

❻ 如何写CMakeList.txt

这一章将从软件开发者的角度来描述如何实用CMake。也就是说,如果你的目标是用CMake来管理你的生成过程,请阅读这一章。

CMake的输入

COMMAND(args)

这里的 COMMAND 是命令行的名称,args是用空格分割的参数列表。典型的,对与每一个项目的目录存在一个CMakeLists.txt。 下面我们将从一个简单的Hello world例子开始介绍, 它的源代码树形文件包含这些文件:

Hello.c CMakeLists.txt

CMakeLists.txt将包含下面两行:

PROJECT(Hello)
ADD_EXECUTABLE(Hello Hello.c)

为了生成Hello的可执行程序,你只需依照上面CMake运行的过程描述来生成makefiles文件。 PROJECT 命令表明了产生的工作空间的名称。 ADD_EXECUTABLE命令添加可执行的目标到生成程序。这个简单的程序就只需要这些设置。如歌你的项目需要一些文件才能编译也很容易,只想修改ADD_EXECUTABLE命令行如下:

ADD_EXECUTABLE(Hello Hello.c File2.c File3.c File4.c)

ADD_EXECUTABLE只是很多CMake命令中的一种。比如更复杂的如下:

PROJECT(HELLO)
SET(HELLO_SRCS Hell.c File2.c File3.c)
IF(WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} WinSupport.c)
ELSE (WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} UnixSupport.c)
ENDIF (WIN32)

ADD_EXECUTABLE (Hello ${HELLO_SRCS})

#look for the Tcl library
FIND_LIBRARY(TCL_LIBRARY NAMES tcl tc184 tc183 tc 182 tc 180
PATHS /usr/lib /usr/local/lib)
IF (TCL_LIBRARY)
TARGET_ADD_LIBRARY (Hello TCL_LIBRARY)
ENDIF(TCL_LIBRARY)

在这个例子中 SET 命令用于将源文件组成一个列表。 IF 命令用于增加WinSupport.c或者UnixSupport.c到列表中。 最后 ADD_EXECUTABLE 命令用于 采用源文件列表HELLO_SRCS中列出的文件 生成可执行文件。FIND_LIBRARY命令用于寻找在一些指定目录下的特定的Tcl库文件。如果找到了,就将他们添加到Hello可执行程序的链接命令。 #行为注释行。

CMake 是会定义一些使用的变量在CMakeList文件中。 比如,WIN32总是会在windows系统中被定义,而UNIX
总是在UNIX系统中被定义。

生成目标:(Build Targets)

SET()
SUBDIRS()
ADD_LIBRARY()
这里生成静态链接文件,例如ADD_LIBRARY(Whole ${HELLO_SRC}),就会生成一个libWhole.a可供链接

ADD_EXECUTABLE()
AUX_SOURCE_DIRECTORY()
PROJECT()

CMake会循环的查找从当前目录到SUBDIRS列出的任何子目录的文件。SET命令用于设定一个变量。ADD_LIBRARY将添加一个库到目标之中。 ADD_EXECUTABLE添加一个可执行程序到目标列表中。(Note:编译器执行的顺序是先编译源文件,然后生成库文件,最后生成可执行文件)。AUX_SOURCE_DIRECTORY表示一个不在当前目录的包含源文件的目录。这些源代码将插入当前的库(LIBRARY)中。所有在AUX_SOURCE_DIRECTORY的文件将被编译(如,*.c,*.cxx,*.cpp等等)。PROJECT(ProjectName)是一个用在MSVC中的特殊变量,用于为编译器生成项目。他也为CMAKE定义连个有用的变量:ProjectName_SOURCE_DIR和ProjectName_BINARY_DIR.

编译的标示和选项。除了上面列出的命令外,CMakeLists.txt还包含如下的命令:
INCLUDE_DIRECTORIES()
LINK_DIRECTORIES()
LINK_LIBRARIES()
TARGET_LINK_LIBRARIES()

这些命令定义了用于编译源代码和生成可执行程序的目录和库。上面列出的目录的一个很重要的特性是它们会被任何子目录继承。也就是说,CMake依照目录的分层结构来承袭这些命令。在每次遇到对这些命令的描述的时候都会被展开一次。比如说,如果在顶层的CMakeLists文件中有定义INCLUDE_DIRECTORIES(/usr/include)和SUBDIRS(./subdir1),并且在./subdir1/CMakeLists.txt有INCLUDE_DIRECTORIES(/tmp/foobar),于是最后网状的结果是
INCLUDE_DIRECTORIES(/usr/include /tmp/foobar)

CMake会定义许多的模块来查找通常会用到的包,比如OpenGL或Java。 这些模块为你节省了很多的时间来编写这些查找包。这些模块可以像这样加到你的CMakeList文件中,如下:

INCLUDE(${CMAKE_ROOT}/Moles/FindTCL.cmake)

CMAKE_ROOT 总是定义在CMake中,用于指向CMake安装的路径。查看Moles子目录下的一些文件可以给你提供一些很好的idea关于怎样用这些CMake命令。

给项目文件添加一个新的目录
一个通用的方法来扩展一个项目文件是给他添加一个新的文件夹。这将包含三个步骤:
1.创建一个新的目录在你的源代码的分层目录中
2.将这个新的目录添加到SUBDIRS命令中
3.在这个新创建的目录中用适当的命令建立一个CMakeLists.txt文件

This section describes how to use CMake from the software developer's point of view. That is, if your aim is to use CMake to manage your build process, read this section first.
Input to CMake

COMMAND(args)
Where COMMAND is the name of the command, and args is a white-space separated list of arguments to the command. (Arguments with embedded white-space should be quoted.) Typically there will be a CMakeLists.txt file for each directory of the project. Let's start with a simple example. Consider building hello world. You would have a source tree with the following files:
Hello.c CMakeLists.txt
The CMakeLists.txt file would contain two lines:
PROJECT (Hello)
ADD_EXECUTABLE(Hello Hello.c)
To build the Hello executable you just follow the process described in Running CMake above to generate the makefiles or Microsoft project files. The PROJECT command indicates what the name of the resulting workspace should be and the ADD_EXECUTABLE command adds an executable target to the build process. That's all there is to it for this simple example. If your project requires a few files it is also quite easy, just modify the ADD_EXECUTABLE line as shown below.
ADD_EXECUTABLE(Hello Hello.c File2.c File3.c File4.c)

ADD_EXECUTABLE is just one of many commands available in CMake. Consider the more complicated example below.

PROJECT (HELLO)
SET(HELLO_SRCS Hello.c File2.c File3.c)
IF (WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} WinSupport.c)
ELSE (WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} UnixSupport.c)
ENDIF (WIN32)
ADD_EXECUTABLE (Hello ${HELLO_SRCS})

# look for the Tcl library
FIND_LIBRARY(TCL_LIBRARY NAMES tcl tcl84 tcl83 tcl82 tcl80
PATHS /usr/lib /usr/local/lib)
IF (TCL_LIBRARY)
TARGET_ADD_LIBRARY (Hello TCL_LIBRARY)
ENDIF (TCL_LIBRARY)
In this example the SET command is used to group together source files into a list. The IF command is used to add either WinSupport.c or UnixSupport.c to this list. And finally the ADD_EXECUTABLE command is used to build the executable with the files listed in the source list HELLO_SRCS. The FIND_LIBRARY command looks for the Tcl library under a few different names and in a few different paths, and if it is found adds it to the link line for the Hello executable target. Note the use of the # character to denote a comment line.
CMake always defines some variables for use within CMakeList files. For example, WIN32 is always defined on windows systems and UNIX is always defined for UNIX systems. CMake defines a number of commands. A brief summary of the most commonly used commands follows here. Later in the document an exhaustive list of all pre-defined commands is presented. (You may also add your own commands, see the Extension Guide for more information.)

Build Targets:

SET()
SUBDIRS()
ADD_LIBRARY()
这里生成静态链接文件,例如ADD_LIBRARY(Whole ${HELLO_SRC}),就会生成一个libWhole.a,可供链接。

ADD_EXECUTABLE()
AUX_SOURCE_DIRECTORY()
PROJECT()
CMake works recursively, descending from the current directory into any subdirectories listed in the SUBDIRS command. The command SET is used for setting a variable, in this case to a list of source files. (Note: currently only C and C++ code can be compiled.) ADD_LIBRARY adds a library to the list of targets this makefile will proce. ADD_EXECUTABLE adds an executable to the list of targets this makefile will proce. (Note: source code is compiled first, then libraries are built, and then executables are created.) The AUX_SOURCE_DIRECTORY is a directory where other source code, not in this directory, whose object code is to be inserted into the current LIBRARY. All source files in the AUX_SOURCE_DIRECTORY are compiled (e.g. *.c, *.cxx, *.cpp, etc.). PROJECT (PojectName) is a special variable used in the MSVC to create the project for the compiler, it also defines two useful variables for CMAKE: ProjectName_SOURCE_DIR and ProjectName_BINARY_DIR.

Build flags and options. In addition to the commands listed above, CMakeLists.txt often contain the following commands:

INCLUDE_DIRECTORIES()
LINK_DIRECTORIES()
LINK_LIBRARIES()
TARGET_LINK_LIBRARIES()
These commands define directories and libraries used to compile source code and build executables. An important feature of the commands listed above is that are inherited by any subdirectories. That is, as CMake descends through a directory hierarchy (defined by SUBDIRS()) these commands are expanded each time a definition for a command is encountered. For example, if in the top-level CMakeLists file has INCLUDE_DIRECTORIES(/usr/include), with SUBDIRS(./subdir1), and the file ./subdir1/CMakeLists.txt has INCLUDE_DIRECTORIES(/tmp/foobar), then the net result is
INCLUDE_DIRECTORIES(/usr/include /tmp/foobar)
CMake comes with a number of moles that look for commonly used packages such as OpenGL or Java. These moles save you from having to write all the CMake code to find these packages yourself. Moles can be used by including them into your CMakeList file as shown below.

INCLUDE (${CMAKE_ROOT}/Moles/FindTCL.cmake)
CMAKE_ROOT is always defined in CMake and can be used to point to where CMake was installed. Looking through some of the files in the Moles subdirectory can provide good ideas on how to use some of the CMake commands.

Adding A New Directory to a project

A common way to extend a project is to add a new directory. This involves three steps:
Create the new directory somewhere in your source directory hierarchy.
Add the new directory to the SUBDIRS command in the parent directories CMakeLists.txt
Create a CMakeLists.txt in the new directory with the appropriate commands

❼ linux的CmakeList.txt怎么写解决多线程唤起同一个文件(多次)

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。只是 CMake 的组态档取名为 CmakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。这使得熟悉某个集成开发环境(IDE)的开发者可以用标准的方式建构他的软件,这种可以使用各平台的原生建构系统的能力是 CMake 和 SCons 等其他类似系统的区别之处。CMake 可以编译源代码、制作程式库、产生适配器(wrapper)、还可以用任意的顺序建构执行档。CMake 支持 in-place 建构(二进档和源代码在同一个目录树中)和 out-of-place 建构(二进档在别的目录里),因此可以很容易从同一个源代码目录树中建构出多个二进档。CMake 也支持静态与动态程式库的建构。

❽ CmakeLists.txt里如何设置编译线程的数量类似-j6

专门用来编译mkdir buildcd build二、cmake指向CMakeLists.txt所在的目录,例如cmake... 至于怎么淘汰,这里不说,但是毕竟是淘汰机制

❾ OSX系统用QT编译cmakelist文件时出错 在这无限循环

Qt在编译时,需要首先使用uic.exe编译UI文件,通过moc.exe处理Q_OBJECT之类的宏,当然你可以去掉这些定义,否则就需要加上:#COMPILEUIsSET(UISmnwindow)FOREACH(ui_file${UIS})SET(TMP_IN"${PROJECT_SOURCE_DIR}/res/${ui_file}.ui")SET(TMP_OUT"${PROJECT_BINARY_DIR}/ui_${ui_file}.h")EXECUTE_PROCESS(COMMAND${QT_FOLDER}/bin/uic.exe-o${TMP_OUT}${TMP_IN})MESSAGE(STATUS"EXEC=${QT_FOLDER}/bin/uic.exe-o${PROJECT_BINARY_DIR}/ui_${ui_file}.h${PROJECT_SOURCE_DIR}/res/${ui_file}.ui")ENDFOREACH(ui_file)#CREATEMOCSET(HEADERSmainwindow)FOREACH(header_file${HEADERS})SET(TMP_IN"${PROJECT_SOURCE_DIR}/inc/${header_file}.h")SET(TMP_OUT"${PROJECT_BINARY_DIR}/moc_${header_file}.cpp")EXECUTE_PROCESS(COMMAND${QT_FOLDER}/bin/moc.exe-o${TMP_OUT}${TMP_IN})SET(MOCS${MOCS}${PROJECT_BINARY_DIR}/moc_${header_file}.cpp)MESSAGE(STATUS"EXEC=${QT_FOLDER}/bin/moc.exe-o${PROJECT_BINARY_DIR}/moc_${header_file}.cpp${PROJECT_SOURCE_DIR}/inc/${header_file}.h")ENDFOREACH(header_file)freec#.netqrcreator这里要注意的是:EXECUTE_PROCESS在COMMAND后面不能加引号,命令直接写,否则执行的程序也不会出错,但也不会正常运行.

热点内容
数字支付密码哪里找 发布:2024-05-04 14:26:50 浏览:84
天翼云免费存储 发布:2024-05-04 14:22:55 浏览:781
微信56g缓存怎么解决 发布:2024-05-04 14:09:41 浏览:705
sqlupdatewhereand 发布:2024-05-04 13:55:47 浏览:586
java视频教程推荐 发布:2024-05-04 13:55:08 浏览:86
安卓官服闪耀暖暖怎么换 发布:2024-05-04 13:46:37 浏览:170
我的世界精灵服务器怎么抓宠物 发布:2024-05-04 13:28:54 浏览:960
编译androidwebkit 发布:2024-05-04 13:11:37 浏览:761
安卓微信流量怎么控制 发布:2024-05-04 12:47:19 浏览:799
mysql主从复制数据库 发布:2024-05-04 12:37:55 浏览:512