Ubuntu下使用cmake生成可執行文件


1.單個CPP文件的cmake

首先編寫一個簡單的程序(main.cpp):

#include <iostream>
using namespace std;
int main()
{
  cout<<"hello cmake"<<endl;
  return 0;
}

編寫 CMakeLists.txt,並與main.cpp 放在同一個目錄。

project(hello)
add_executable(hello main.cpp)

進入CMakeLists.txt所在目錄,在CMakeLists.txt 所在的目錄下創建一個 build目錄,進入build目錄輸入:

cmake ..
make

執行完畢后,在 build 目錄下會有很多編譯出來的文件,但是只需要其中的可執行文件,如果要把可執行文件放到與 build 目錄同級的 bin 文件夾下,那么在CMakeLists.txt 的最后加上下面這兩句話:

set( CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/bin/")
install(TARGETS hello RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

將 make 指令改為make install,執行完畢后,就可以在 bin 文件夾中看到可執行文件 hello。

注意:上述語句不能直接寫成:

install(TARGETS hello RUNTIME DESTINATION bin)

會產生錯誤:

CMake Error at cmake_install.cmake:42 (FILE):
  file INSTALL cannot copy file “XXXX” to "XXXX"

因為默認路徑前綴為/usr/local/

若不手動改路徑前綴則會出現將項目install到/usr/local/bin而沒有權限的情況。

2.引入data文件夾

如果可執行文件需要調用一些文件,假設文件在 data 文件夾中,且 data 與CMakeLists.txt 在同級目錄,則可以通過如下語句將 data 文件夾也放入 bin 文件夾

install(DIRECTORY data DESTINATION ${CMAKE_INSTALL_PREFIX})

3.多個CPP和H文件的cmake

如果 main.cpp 中用到了另一個文件的內容,文件目錄如下圖,應該怎么辦呢?


其中 hello.h 為類的聲明,hello.cpp 為類函數的定義。
這時可以把 CMakeLists.txt 改為下面這樣:

project(hello)
set(SRCS main.cpp hello.cpp)
add_executable(hello ${SRCS})

上述代碼等價於:

project(hello)
add_executable(hello main.cpp hello.cpp)

如果 hello.cpp 與 main.cpp 不在同一個文件夾呢?如下所示


這時候每個文件夾下都要有一個 CMakeLists.txt。
其中 main.cpp 同級目錄下的 CMakeLists.txt 如下:

project(hello)
add_executable(hello main.cpp)
target_link_libraries(hello header)

Hello 目錄下的 CMakeLists.txt 如下:

add_library(header STATIC hello.cpp)

說明:第一個 CMakeLists.txt 中的 add_subdirectory(Hello)表示在編譯時要包含Hello這個目錄,而 target_link_libraries(hello header)表示在生成 hello 時要調用靜態鏈接庫 header ,而 header 是在 Hello 目錄下的 CMakeLists.txt 中的add_library(header STATIC hello.cpp)中產生的。

同理,如果 hello.cpp 中使用了其它鏈接庫里的東西,則在其 CMakeLists.txt 也要加入 target_link_libraries(header xxx)。
注意:在寫 target_link_libraries 語句時,括號里面的第一名稱必須是當前所需要生成文件的名稱,比如上面的 hello 和 header。但是,這樣寫在引用頭文件時要寫成 #include “Hello/hello.h”, 顯得過長,這時,可以在 CMakeLists.txt 中加入下面這句話:

include_directories(./Hello)

這樣引用頭文件就可以寫成 #include “hello.h”
更多使用方法自行百度。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM