新建工程並用cmake編譯和安裝

2021-09-25 07:25:00 字數 2679 閱讀 4978

本文介紹新建標準的cmake工程及使用cmake編譯、cmakelists的編寫、編譯和安裝標準的cmake工程。

(1)cmake工程目錄結構

在cmake工程中有不同的資料夾。資料夾結構通常如下。

(2)資料夾介紹

src 原始檔所在位置的目錄

include 標頭檔案所在位置的目錄

build 編譯生成的檔案所在的目錄

hellofunlib資料夾 為hellofun庫函式原始檔所在資料夾

bin 生成的主函式所在的資料夾

lib 生成的庫函式所在資料夾

任務:

(1)編譯main函式生成cmakehello可執行檔案,並放在/build/bin檔案下

(2)編譯hellofuc.c生成 動態鏈結庫函式檔案hellofuc.so,並放在/build/lib資料夾下。

實現:

(a)頂層/cmakelists.txt

cmake_minimum_required (version 2.6)

add_subdirectory(src)

add_subdirectory(hellofunlib)

message(status "this is project_binary_dir "

$)message(status "this is project_binary_dir "

$)#指定可執行檔案和庫檔案目錄

set(executable_output_path $/bin)

set(library_output_path $/lib)

(b)主函式/src/main.c

#include 

int main()

(c)src資料夾src/cmakelists.txt

add_executable(cmakehello main.c)

(d)庫函式資料夾函式/hellofunlib/hellofun.c

#include 

#include "hellofun.h"

void hello()

(e)庫函式資料夾標頭檔案/hellofunlib/hellofun.h

#ifndef hellofun_h

#define hellofun_h

#include

void hellofunc();

#endif

(f)庫函式資料夾hellofunlib/cmakelists.txt

add_library(hellofun shared hellofun.c)

最後切換到/bulid目錄下

輸入命令

cmake ..

make

發現在/build/src目錄下生成可執行檔案cmakehello

在/build/bin目錄下生成庫函式檔案libhellofun.so

任務:

將生成可執行檔案cmaketest安裝到/home/programfiles/bin

生成庫函式檔案libhellofun.so 安裝到/home/programfiles/lib

(a)在頂層 /cmakelists.txt

加入set(cmake_install_prefix /home/programfiles)

(b)在src資料夾 src/cmakelists.txt

加入

install

(targets cmakehello runtime

destination bin)

(c)在庫函式資料夾hellofunlib/cmakelists.txt加入

install

(targets hellofun library

destination lib)

然後切換到build資料夾下

cmake ..

make

make install

可以發現

生成可執行檔案cmaketest已經安裝到/home/programfiles/bin

庫函式檔案libhellofun.so 已經安裝到/home/programfiles/lib

本文介紹新建標準的cmake工程及使用cmake編譯、cmakelists的編寫、編譯和安裝標準的cmake工程。

新建工程並用cmake編譯和安裝

本文介紹新建標準的cmake工程及使用cmake編譯 cmakelists的編寫 編譯和安裝標準的cmake工程。1 cmake工程目錄結構 在cmake工程中有不同的資料夾。資料夾結構通常如下。2 資料夾介紹 src 原始檔所在位置的目錄 include 標頭檔案所在位置的目錄 build 編譯生...

CMake構建工程

目標 使用cmake構建多級的目錄的c 專案,包含動態庫的引用。檔案結構 cmakelists.txt 1 build lib 這裡存放動態庫 cmakelists.txt 2 lib bye cmakelists.txt 3 bye.cc 4 lib hello cmakelists.txt 5 ...

pycharm新建工程

建立乙個first的project,將其設定為source root.並且新建兩個資料夾,hello 和 test,這裡的資料夾相當於 package 也可以使用new pathon package 建立,建立好之後會有相應的init.py檔案,分別在資料夾下編寫 hellomodule.py 和 ...