muduo庫學習筆記三 cmake使用解析

2021-10-01 11:53:21 字數 4933 閱讀 6914

在linux平台下使用cmake生成makefile並編譯的流程如下:

a、編寫cmake配置檔案cmakelists.txt

b、執行命令cmake path生成makefile,path是cmakelists.txt所在的目錄。

c、使用make命令進行編譯。

其中,a既是下文注釋的檔案,b、c都由muduo庫中提供的build.sh完成,我們使用時只需要執行build.sh,這個bash指令碼會幫我們完成b、c。

cmake_minimum_required(version 2.6)

#cmake最小版本為2.6

project(muduo c cxx)

#project 用這個 定義工程名稱,並指定工程支援的語言(可忽略)預設支援所有語言

enable_testing()

#設定生成debug版本或者release版本

if(not cmake_build_type)

set(cmake_build_type "debug")

# set(cmake_build_type "release")

endif()

#set指令可以用來顯示的定義變數

#在cmake指令碼中,設定編譯選項可以通過add_compile_options命令,也可以通過set命令修改cmake_cxx_flags或cmake_c_flags。

#使用這兩種方式在有的情況下效果是一樣的,但請注意它們還是有區別的:

#add_compile_options命令新增的編譯選項是針對所有編譯器的(包括c和c++編譯器),

#而set命令設定cmake_c_flags或cmake_cxx_flags變數則是分別只針對c和c++編譯器的。

set(cxx_flags

-g # -dvalgrind

# -dmuduo_std_string

-dcheck_pthread_return_value

-d_file_offset_bits=64

-wall #大部分警告

-wextra #一些額外的警告

-werror #當出現警告時轉為錯誤,停止編譯

-wconversion #一些可能改變值的隱式轉換,給出警告

-wno-unused-parameter #函式**現未使用的引數,不給出警告

-wold-style-cast #c風格的轉換,給出警告

-woverloaded-virtual #如果函式的宣告隱藏住了基類的虛函式,就給出警告

-wpointer-arith #對函式指標或者void *型別的指標進行算術操作時給出警告

-wshadow #當乙個區域性變數遮蓋住了另乙個區域性變數,或者全域性變數時,給出警告

-wwrite-strings #規定字串常量的型別是const char[length],因此,把這樣的位址複製給 non-const char *指標將產生警告.這些警告能夠幫助你在編譯期間發現企圖寫入字串常量的**

-march=native #指定cpu體系結構為本地平台

# -mmd

# -std=c++0x

-rdynamic

)if(cmake_build_bits equal 32)

#新增新element到list中

endif()

string(replace ";" " " cmake_cxx_flags "$")

set(cmake_cxx_compiler "g++")

set(cmake_cxx_flags_debug "-o0")

set(cmake_cxx_flags_release "-o2 -finline-limit=1000 -dndebug")

set(executable_output_path $/bin)

set(library_output_path $/lib)

#executable_output_path 設定二進位制的輸出路徑

#library_output_path 設定庫的輸出的路徑

#cmake_cxx_compiler 指定cxx編譯器

#cmake_build_type ,可以的取值是 debug release relwithdebinfo

#和 minsizerel。當這個變數值為 debug 的時候,cmake 會使用變

#量 cmake_cxx_flags_debug 和 cmake_c_flags_debug 中的字串

#作為編譯選項生成 makefile ,當這個變數值為 release 的時候,

#工程會使用變數 cmake_cxx_flags_release 和 cmake_c_flags_release 選項

#生成 makefile。

#project_binary_dir = 如果是 in source 編譯,指得就是工程頂層目錄,如果是 out-of-source 編譯,指的是工程編譯發生的目錄。

#cmake_install_prefix 用於定義相對安裝路徑在build.sh中定義

find_package(boost required)

find_package(protobuf)

find_package(curl)

find_package(zlib)

find_path(cares_include_dir ares.h)

#cares_include_dir 代表找到的ares.h的全路徑包括檔名

#message指令用於向終端輸出使用者定義的資訊,

#有三種型別:

#send_error 產生錯誤,生產過程被跳過

#status 輸出字首為-的資訊

#fatal_error,立即終止所有cmake過程

if(cares_include_dir and cares_library)

message(status "found cares")

endif()

if(curl_found)

message(status "found curl")

endif()

if(protobuf_found)

message(status "found protobuf")

endif()

if(tcmalloc_include_dir and tcmalloc_library)

message(status "found tcmalloc")

endif()

if(zlib_found)

message(status "found zlib")

endif()

if(hiredis_include_dir and hiredis_library)

message(status "found hiredis")

endif()

if(gd_include_dir and gd_library)

message(status "found gd")

endif()

if(thrift_compiler and thrift_include_dir and thrift_library)

message(status "found thrift")

endif()

include_directories($)

include_directories($)

string(toupper $ build_type)

message(status "cxx_flags = " $ " " $})

#包含子目錄

add_subdirectory(muduo/base)

#add_subdirectory(muduo/net)

#if(not cmake_build_no_examples)

# add_subdirectory(contrib)

# add_subdirectory(examples)

#else()

# if(cares_include_dir and cares_library)

# add_subdirectory(examples/cdns)

# endif()

#endif()

#包含子目錄

add_subdirectory(tests)

#子目錄下的 cmakelists.txt檔案和源**也會被處理 。

muduo庫學習筆記 執行緒安全

muduo規定的必須在io執行緒完成的操作 連線的更新 連線的讀寫 連線的關閉 連線的析構等 tcpserver start 將acceptor listen置於所屬執行緒?一直不理解為什麼這裡需要將listen放到其所屬執行緒中 因為listen的描述符一定是在io執行緒中的 可能原因是其他從re...

muduo庫學習筆記七 base庫之Mutex

mutex互斥量 muduo庫中的封裝為mutexlock類與mutexlockguard類。類圖 繼承boost noncopyable,物件語義不能拷貝,該類主要是對一系列系統執行緒函式pthread mutex 的封裝。私有資料成員 mutex 儲存linux執行緒互斥量pthread mut...

muduo 日誌庫學習 二

logfile類和asynclogging類各有自己的buffer 在下文中,分別記為file buffer和async buffer 當使用者使用log 寫入日誌內容時,將會把日誌內容寫入到async buffer中,當async buffer寫滿了,就會把async buffer中的內容寫入到f...