根據網上的資料,我總結了一下,一個工程應該有根目錄(bin)存放可執行文件,頭文件目錄(include)存放頭文件,源碼文件(src)存放你的算法,還須要一個庫文件夾存放你編譯的靜態庫或者動態庫。而後得出下面這個例子:html
/home/bmi-zc/project:
|—CMakeLists.txt 頂層CMakeLists
|
|—bin
|
|—include
| test.h
|
|—lib
|
|—src
| CMakeLists.txt
|
|—main
| CMakeLists.txt
| main.c
|
|—test
CMakeLists.txt
test1.c
test2.c 算法
test.h編輯器
#ifndef INCLUDE_TEST_H #define INCLUDE_TEST_H void t1(); void t2(); #endif /*INCLUDE_TEST_H*/
test1.cui
#include "/home/bmi-zc/project/include/test.h" #include <stdio.h> void t1() { printf("this is t1()\n"); }
test2.cthis
#include "/home/bmi-zc/project/include/test.h" #include <stdio.h> void t2() { printf("this is t2()\n"); }
main.c:.net
#include "/home/bmi-zc/project/include/test.h" int main() { t1(); t2(); return 0; }
/home/bmi-zc/project/CMakeLists.txt:3d
cmake_minimum_required(VERSION 3.5) PROJECT(TEST) ADD_SUBDIRECTORY(src)
CMakeLists.txt內容:指定項目名稱,添加工程源碼子目錄(子目錄裏面還有目錄,只須要把源碼根目錄添加進去就行,例如上圖,只須要添加src目錄ADD_SUBDIRECTORY(src)
便可)。
用到的指令:PROJECT,ADD_SUBDIRECTORY。code
/home/bmi-zc/project/src/htm
ADD_SUBDIRECTORY(main) ADD_SUBDIRECTORY(test)
CMakeLists.txt內容:添加源碼子目錄
所用到指令:ADD_SUBDIRECTORYblog
/home/bmi-zc/project/src/test/
SET(CMAKE_C_COMPTLER GCC) SET(SRC_LIST test1.c test2.c) INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) ADD_LIBRARY(libtest STATIC ${SRC_LIST})
CMakeLists.txt內容:設置編輯器,添加頭文件路徑,設置lib輸出目錄,生成靜態連接庫
/home/bmi-zc/project/src/main
SET(SRC_LIST main.c) INCLUDE_DIRECTORIES(${TEST_SOURCE_DIR}/include) SET(EXECUTABLE_OUTPUT_PATH ${TEST_SOURCE_DIR}/bin) ADD_EXECUTABLE(main ${SRC_LIST}) link_directories(${TEST_SOURCE_DIR}/lib) TARGET_LINK_LIBRARIES(main libtest)
CMakeLists.txt內容:整理代碼清單,頭文件目錄,可執行文件輸出路徑,靜態庫連接路徑,連接靜態庫。
回到工程根目錄,/home/bmi-zc/project
cmake .
make
進入bin文件夾,執行main可執行文件
cd bin
./main