當前位置:首頁 » 編程軟體 » 用vs編譯cmakelist

用vs編譯cmakelist

發布時間: 2022-04-22 05:53:51

1. 如何用cmake 編譯OpenCV 3.1.0的opencv

你機子的某個盤里建一個新的文件夾,如我這里的完整路徑是:D:\CMake\CMake-Study\HelloCMake; 然後在HelloCMake文件夾里建立一個HelloCMake.cpp文件,裡面的代碼如下:
#include <iostream>
int main()
{
std::cout<<"Study CMake Together - Hello CMake!"<<std::endl;
return 0;
}
然後在HelloCMake文件夾里建立一個CMakeLists.txt文件,注意文件名不能是別的,必須叫這個名字。裡面的內容如下:
cmake_minimum_required(VERSION 2.6)
project(HelloCmake)
add_executable(HelloCMake hellocmake.cpp)
接著在HelloCMake的同級目錄里建立一個新的文件夾,就叫做HelloCMake-bin,在我機子上的完整路徑是:D:\CMake\CMake-Study\HelloCMake-bin,你可以針對自己的目錄路徑對號入座。
然後打開CMake程序,界面如圖(1):

圖(1)

圖(2)
接著把包含CMakeLists.txt和HelloCMake.cpp文件的完整路徑給」where is the source code」,把最後建立的HelloCMake-bin目錄的完整路徑給」where to build the binaries」,然後按Cofigure,界面如圖(2)。
Configure完了以後,再按Generate,直到所有的紅色選項都變成灰色為止。界面如圖(3)。然後打開HelloCMake-bin,你會發現原來空的文件夾裡面自動生成了好多文件,如圖(4)。

圖(3)

圖(4)
到此為止,工程構建完成,打開HelloCMake-bin目錄下的HelloCMake.sln,裡面有三個工程,分別是:ALL_BUILD;HelloCMake;ZERO_CHECK。這三個工程的大概作用如下(個人理解而已),HelloCMake就不用說了,自己要建立的那個工程;ALL_BUILD是管理整個項目的工程;ZERO_CHECK是實時監視CMakeLists.txt文件變化的工程,一旦CMakeLists.txt里的內容發生了任何變化,ZERO_CHECK就會告訴編譯器要重新構建整個工程環境。所以,你可以先把工程關掉,打開CMakeLists.txt文件,更改裡面的內容以後,把根據以上說的步驟走一遍CMake;你也可以在編譯器環境(如VS2008)中更改CMakeLists.txt文件,然後直接F7編譯工程。如果你選擇後者,你會發現如圖(5)所示的現象發生。
因為你改變了CMakeLists.txt的內容,工程的環境要重新構建、設置。所以你必須載入新的工程環境設置。點Yes,然後點Reload。重新載入設置過的工程環境。
另外一個經常看某些童鞋問的,就是當彈出圖(6)所示的界面時要怎麼辦?能怎麼辦?界面上提示已經灰常清楚了,指定exe的路徑給它,然後點OK就可以了;出現這種情況的原因是你把沒有生成exe可執行文件的工程設為啟動工程了,如圖(6)所示,你會發現ALL_BUILD這個工程名字是黑體顯示的,表示它是啟動工程;或者你把生成exe可執行文件的工程,如這里的HelloCMake右擊->設置為啟動工程,也不會出現如圖(6)所示的界面。
好,這些比較瑣碎的東西介紹完,我們一起來看看CMakeLists.txt裡面的代碼表示什麼意思,爭取每句代碼都作解釋,如果有不對的地方,一定要告訴我!

圖(5)

圖(6)
把CMakeLists.txt里的內容再羅列出來:
1. cmake_minimum_required(VERSION 2.6)
2. project(HelloCmake)
3. add_executable(HelloCMake hellocmake.cpp)
第1行,cmake_minimum_required(VERSION 2.6)這是對CMake版本的要求,基本上每個CMakeLists.txt文件里都會有這句代碼,cmake_minimum_required是cmake里的命令,可大寫小寫。VERSION這個關鍵字必須是大寫,而且不能省略;2.6就是CMake的版本號,現在的版本是2.8.3。
第2行,project(HelloCmake),project也是CMake的命令,裡面的參數HelloCMake是你要生成的工程的名字,換句話說就是生成的***.sln或者***.dsw等工程項目文件的名字。
第3行,add_executable(HelloCMake hellocmake.cpp),add_executable同樣是CMake的命令,鏈接有關的源文件,然後生成exe可執行文件,這是這個命令的作用。第一個參數是生成的exe文件的文件名,一般與project里的工程名一致,這樣編譯生成的文件就分別是HelloCMake.sln和HelloCMake.exe,當然也可以不一樣。Add_executable()後面的是一個參數列表,可帶多個要編譯的文件名,中間以空格或回車等隔開,如可以加入:
add_executable(HelloCMake hellocmake.cpp hellocmake.h)
這樣就把CMake里最常用的三個命令介紹完了,分別是cmake_minimum_required; project; add_executable等

2. 如何寫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

3. 用cmake編譯的代碼在vs里怎麼運行

代碼要編譯了才能運行,編譯的這個工作需要一組工具來完成 對我有用[0] 丟...選了VS 9 2008 Win64以後,Cmake這一關就過去了。

4. Visual Studio中用cmake編譯代碼

opencv是第三方的庫,你得自己下載導進項目里才能用

5. vs2017怎麼用內置CMAKE編譯opencv

使用opencv需要編譯源碼,得到庫文件。 可以用cmake構建項目後編譯,也可以直接用官方提供的編譯好的版本。 官方提供的編譯庫一般只是標准版本,可能與某些庫並不兼容, 比如官方提供的編譯好的版本與opengl就不兼容,這時就只能自己構建項目後編vs2017怎麼用內置CMAKE編譯opencv

6. 如何用vs運行cmake後的工程

1、單個文件示例:
1) 首先建立文件夾CMakeTest/Src
2) 在文件夾Src中建立兩個文件main.c和CMakeLists.txt
3) main.c:
#include <stdio.h>

int main()
{
printf("hello world.");
getchar();
return 0;
}
4) CMakeLists.txt
PROJECT (HELLO)

SET (SRC_LIST main.c)

ADD_EXECUTABLE (hello ${SRC_LIST})
5) 打開:開始--Microsoft Visual Studio 2012--Visual Studio Tools--VS2012 x86 Native Tools Command Prompt
6) 進入到CMakeTest/build/Src目錄,執行以下代碼
mkdir build & cdbuild
cmake .. -G"NMake Makefiles"
nmake

7. 怎樣編寫Cmake的配置文件Cmakelist.txt

CMake是一個編譯配置工具, 它是一個跨平台c/c++ 編譯配置工具。可以通過編寫CMakeLists.txt配置文件,可以控制生成的Makefile或者windows下工程文件。還支持安裝(make install)、測試安裝的程序是否能正確執行(make test,或者ctest)、生成當前平台的安裝包(make package)、生成源碼包(make package_source)、產生Dashboard顯示數據並上傳等高級功能,只要在CMakeLists.txt中簡單配置,就可以完成很多復雜的功能,包括寫測試用例。

如果有嵌套目錄,子目錄下可以有自己的CMakeLists.txt。

所以寫好CMakeLists.txt 是使用好Cmake的關鍵

工具/原料
Cmake
方法/步驟
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文件內容:
6

查看編譯後結果:
7

安裝後目錄結構:

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:336
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:378
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:612
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:945
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:742
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:803
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:511
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:372