UBuntu CMake工程配置基礎 - sheismylife的專欄 - 博客頻道 - CSDN.NET
?
install CMake
我用CMake并不關注它的跨平臺特性,因為我只專注于64位 Linux C++ server領域。
sudo apt-get install cmake
chenshu@chenshu-ubuntu:~$ cmake —version
cmake version 2.8.3HelloWorld工程
mkdir -p examples/helloworld
cd examples/helloworld
創建main.cpp 文件,代碼如下:
- #include?<stdio.h> ??
- int ?main()??
- {??
- ????printf( "Hello?World?from?Main!\n" );??
- ???? return ?0;??
- }??
創建CMakeLists.txt文件,配置如下:
- PROJECT?(HELLOWorld)??
- SET(SRC_LIST?main.cpp)??
- MESSAGE(STATUS?"This?is?BINARY?dir?"?${HELLO_BINARY_DIR})??
- MESSAGE(STATUS?"This?is?SOURCE?dir?"${HELLO_SOURCE_DIR})??
- ADD_EXECUTABLE(hello?${SRC_LIST})??
在同目錄下,運行cmake .
chenshu@chenshu-ubuntu:~/Ubuntu One/c++/cmake/examples/helloworld$ cmake .
— The C compiler identification is GNU
— The CXX compiler identification is GNU
— Check for working C compiler: /usr/bin/gcc
— Check for working C compiler: /usr/bin/gcc — works
— Detecting C compiler ABI info
— Detecting C compiler ABI info - done
— Check for working CXX compiler: /usr/bin/c++
— Check for working CXX compiler: /usr/bin/c++ — works
— Detecting CXX compiler ABI info
— Detecting CXX compiler ABI info - done
— This is BINARY dir /home/chenshu/Ubuntu One/c++/cmake/examples/helloworld
— This is SOURCE dir /home/chenshu/Ubuntu One/c++/cmake/examples/helloworld
— Configuring done
— Generating done
— Build files have been written to: /home/chenshu/Ubuntu One/c++/cmake/examples/helloworld
Makefile以及其他一些文件被cmake生成了。執行make命令,hello二進制文件被編譯出來。運行./hello,可以看到結果。
Hello World from Main!
make VERBOSE=1 可以看到詳細的編譯過程。
make clean 就可以清理工程外部構建
HelloWorld采用內部構建,cmake產生的代碼和自己的源代碼文件在同一個目錄,非常不好。因此需要采用cmake的外部構建方式。
創建helloworld2目錄
這次創建一個src目錄存放源代碼,doc目錄存放項目文檔,
CMakeLists.txt需要出現在項目根目錄和src目錄中。
項目根目錄下的內容如下:
project (HelloWorld2)
add_subdirectory(src bin)
src目錄下內容如下:
add_executable(hello2 main.cpp)
創建一個build目錄
cd build
cmake ..
make
build/bin下會找到hello2可執行文件。支持gdb調試
在src/CMakeLists.txt文件中添加一行: set(CMAKE_BUILD_TYPE Debug)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
