CMake 例子五六

2021-07-09 05:09:50 字數 1565 閱讀 6702

例子五

前面還是有一點不爽:如果想讓可執行檔案在 bin 目錄,庫檔案在 lib 目錄怎麼辦?

就像下面顯示的一樣:

+ build/

|+--+ bin/

|  |

|  /--- hello.exe

|/--+ lib/

|/--- hello.lib

•一種辦法:修改頂級的 cmakelist.txt 檔案

project(hello)

add_subdirectory(src bin)

add_subdirectory(libhello lib)

不是build中的目錄預設和源**中結構一樣麼,我們可以指定其對應的目錄在build中的名字。

這樣一來:build/src 就成了 build/bin 了,可是除了 hello.exe,中間產物也進來了。還不是我們最想要的。

•另一種方法:不修改頂級的檔案,修改其他兩個檔案

src/cmakelist.txt 檔案

set(lib_src hello.c)

add_library(libhello $)

set(library_output_path $/lib)

set_target_properties(libhello properties output_name "hello")

例子六在例子三至五中,我們始終用的靜態庫,那麼用動態庫應該更酷一點吧。 試著寫一下

如果不考慮windows下,這個例子應該是很簡單的,只需要在上個例子的 libhello/cmakelist.txt 檔案中的add_library命令中加入乙個shared引數:

add_library(libhello shared $)可是,我們既然用cmake了,還是兼顧不同的平台吧,於是,事情有點複雜:

•修改 hello.h 檔案

#ifndef dbzhang_hello_

#define dbzhang_hello_

#if defined _win32

#if libhello_build

#define libhello_api __declspec(dllexport)

#else

#define libhello_api __declspec(dllimport)

#endif

#else

#define libhello_api

#endif

libhello_api void hello(const char* name);

#endif //dbzhang_hello_•修改 libhello/cmakelist.txt 檔案

set(lib_src hello.c)

add_definitions("-dlibhello_build")

add_library(libhello shared $)

set(library_output_path $/lib)

set_target_properties(libhello properties output_name "hello")

恩,剩下來的工作就和原來一樣了。

Cmake構建OpenCV測試例子

適用cmake的cmakelists.txt來編譯乙個opencv的測試例子。cmakelists.txt的寫法為 project main 新增標頭檔案路徑 include directories usr local opencv347 include usr local opencv347 in...

Qt pro轉CMake工程例子

去掉一些注釋後 qt core gui greaterthan qt major version,4 qt widgets target qtdemo defines qt deprecated warnings sources main.cpp widget.cpp headers widget....

五六單元練習

五 六單元練習 1.新建使用者組,shengchan,caiwu,jishu 2.新建使用者要求如下 tom 是shengchan組的附加使用者 harry 是caiwu組的附加使用者 leo 是jishu組的附加使用者 新建admin使用者,此使用者不屬於以上提到的三個部門 3.新建目錄要求如下 ...