當前位置:編程學習大全網 - 源碼下載 - 如何用 Makefile 反向生成 CMakeLists 文件

如何用 Makefile 反向生成 CMakeLists 文件

CMake是壹個跨平臺的安裝(編譯)工具,可以用簡單的語句來描述所有平臺的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project文件,能測試編譯器所支持的C++特性。只是 CMake 的組態檔取名為 CmakeLists.txt。Cmake 並不直接建構出最終的軟件,而是產生標準的建構檔(如 linux 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然後再依壹般的建構方式使用。

在 linux 平臺下使用 CMake 生成 Makefile 並編譯的流程如下:

編寫 CmakeLists.txt。

執行命令 “cmake PATH” 或者 “ccmake PATH” 生成 Makefile ( PATH 是 CMakeLists.txt 所在的目錄 )。

使用 make 命令進行編譯

工程實例:

壹. 編寫各層CMakeLists.txt

主目錄的主程序main.cpp

#include "hello.h"

extern Hello hello;

int main()

{

hello.Print();

return 0;

}

主目錄的CMakeLists.txt

# to the root binary directory of the project as ${MAIN_BINARY_DIR}.

project (MAIN)

#version support

cmake_minimum_required(VERSION 2.8)

# Recurse into the "Hello" and "Demo" subdirectories. This does not actually

# cause another cmake executable to run. The same process will walk through

# the project's entire directory structure.

add_subdirectory (Hello)

add_subdirectory (Demo)

# Make sure the compiler can find include files from our Hello library.

include_directories (${MAIN_SOURCE_DIR}/Hello)

# Make sure the linker can find the Hello Demo library once it is built.

link_directories (${HELLO_BINARY_DIR}/Hello)

link_directories (${HELLO_BINARY_DIR}/Demo)

#define the source coedes of current directory as DIR_SRCS

AUX_SOURCE_DIRECTORY(. DIR_SRCS)

# Add executable called "MAIN" that is built from the source files

add_executable (Main ${DIR_SRCS})

# Link the executable to the Hello Demo library.

target_link_libraries (Main Hello Demo)

定義項目名project(MAIN),使得當前目錄可以用${MAIN_SOURCE_DIR},由於有2個子目錄,所以需要add_subdirectory它們。由於主程序會使用到其他庫,因而也需要指定連接庫所在目錄。

主目錄下的作用是利用add_executable將當前目錄下的源文件編譯成Main程序,然後通過target_link_libraries鏈接Hello和Demo庫。由於主程序文件使用了hello.h文件,所以要include_directories該目錄。

---------------------------------------------------------------------------------------------------

子目錄Demo的子程序demo.c

#include "hello.h"

Hello hello;

子目錄Demo的CMakeLists.txt

# Make sure the compiler can find include files from our Hello library.

include_directories (${MAIN_SOURCE_DIR}/Hello)

#define the source coedes of current directory as DIR_DEMO_SRCS

AUX_SOURCE_DIRECTORY(. DIR_DEMO_SRCS)

# Add library called "Demo" that is built from the source files

add_library (Demo ${DIR_DEMO_SRCS})

Demo目錄下的CMakeLists主要作用是利用add_library將當前目錄源碼編譯成Demo庫,由於該庫使用到hello.h文件,所以要include_directories該目錄。

---------------------------------------------------------------------------------------------------

子目錄Hello的子程序hello.h

#ifndef _hello_h

#define _hello_h

class Hello

{

public:

void Print();

};

#endif

子目錄Hello的子程序hello.c

#include "hello.h"

#include <stdio.h>

void Hello::Print()

{

printf("Hello, World!\n");

}

子目錄Hello的CMakeLists.txt

#define the source coedes of current directory as DIR_HELLO_SRCS

AUX_SOURCE_DIRECTORY(. DIR_HELLO_SRCS)

# Add library called "hello" that is built from the source files

add_library (Hello ${DIR_HELLO_SRCS})

Hello目錄下的CMakeLists主要作用是利用add_library將當前目錄源碼編譯成Hello庫。

---------------------------------------------------------------------------------------------------

二. 執行cmake命令

至此我們完成了項目中所有 CMakeLists.txt 文件的編寫,進入目錄 step2 中依次執行命令

#cmake .

默認當前目錄,生產makefile

#make

最後編譯程序

  • 上一篇:因為什麽原因,《瘋狂的外星人》血虧六億?
  • 下一篇:怎麽把圖片文件在SQL server數據庫中保存為二進制記錄?(註:不是用圖片路徑),最好有源代碼。
  • copyright 2024編程學習大全網