CMake使用入门
一、开胃菜
hello目录下的文件结构:
├── CMakeLists.txt ├── hello.c ├── hello.h └── main.c
C代码见下节。
最简单的cmake配置文件:
project(HELLO) set(SRC_LIST main.c hello.c) add_executable(hello ${SRC_LIST})
如果要编译成gdb可调试的debug版本,则在配置文件中加入:
set(CMAKE_BUILD_TYPE Debug)
如果要编译成可用gprof分析的版本,则在配置文件中加入:
set(CMAKE_BUILD_TYPE Profile) set(CMAKE_CXX_FLAGS_PROFILE "-pg")
最简单的编译过程(在hello目录中编译):
cmake . make
这样就会在hello目录中生成可执行文件hello和其他cmake相关的配置文件。
为了让代码整洁,可以使用所谓的out-of-source编译方式:
mkdir build cd build cmake .. make
这样编译产生的所有文件都在build中了。
二、一个小工程hello示范
本例主要包含独立库文件、可执行文件的编译和安装过程中涉及的相关问题。
hello目录中的文件结构:
├── CMakeLists.txt ├── libhello │ ├── CMakeLists.txt │ ├── hello.c │ └── hello.h └── src ├── CMakeLists.txt └── main.c
文件内容如下:
project(HELLO) cmake_minimum_required(VERSION 2.8) set(CMAKE_INSTALL_PREFIX /tmp/hello) add_subdirectory(libhello) add_subdirectory(src)
set(CMAKE_INSTALL_PREFIX:设置程序的安装目录,优先级比cmake命令参数设置高。
set(LIB_SRC hello.c) add_definitions("-DLIBHELLO_BUILD") set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) add_library(libhello SHARED ${LIB_SRC}) add_library(hello_static STATIC ${LIB_SRC}) install(TARGETS libhello LIBRARY DESTINATION lib) install(TARGETS hello_static ARCHIVE DESTINATION lib) install(FILES hello.h DESTINATION include) set_target_properties(libhello PROPERTIES OUTPUT_NAME "hello") set_target_properties(hello_static PROPERTIES OUTPUT_NAME "hello")
(1)add_library:生成库文件,SHARED表示动态链接库;
(2)set(LIBRARY_OUTPUT_PATH:生成的库文件路径,PROJECT_BINARY_DIR和CMAKE_BINARY_DIR、<projectname>_BINARY_DIR都是指进行编译的目录;
(3)install:安装头文件和库文件到相应的目录;
(4)set_target_properties(libhello:生成的库文件名为libhello.so,若没有这一行,库文件名就是liblibhello.so。
include_directories(${PROJECT_SOURCE_DIR}/libhello) link_directories(${CMAKE_INSTALL_PREFIX}/lib) aux_source_directory(. APP_SRC) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) add_executable(hello ${APP_SRC}) target_link_libraries(hello libhello) install(TARGETS hello RUNTIME DESTINATION bin) set_property(TARGET hello PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)
(1)include_directories:指定头文件路径;
(2)link_directories:指定库文件路径;
(3)aux_source_directory:将当前目录(.)中的所有文件名赋值给APP_SRC;
(4)set(EXECUTABLE_OUTPUT_PATH:生成的可执行文件的路径,PROJECT_BINARY_DIR和CMAKE_BINARY_DIR、<projectname>_BINARY_DIR都是指进行编译的目录;
(5)add_executable:生成可执行文件,PROJECT_SOURCE_DIR和CMAKE_SOURCE_DIR、<projectname>_SOURCE_DIR都是工程的顶级目录;
(6)target_link_libraries:libhello要和./libhello/CMakeLists.txt中的libhello对应;
(7)install(TARGETS:安装程序到${CMAKE_INSTALL_PREFIX}/bin目录;
(8)set_property:设定安装的可执行文件所需的库文件路径,如果没有该项设置,会出错:cannot open shared object file: No such file or directory。
#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_
#include <stdio.h> #include "hello.h" void hello(const char *name) { printf ("Hello %s!\n", name); }
#include "hello.h" int main(int argc, char *argv[]) { hello("World"); return 0; }
在hello目录下编译程序:
mkdir build cd build cmake .. -DCMAKE_INSTALL_PREFIX=/tmp make make install
(1)-DCMAKE_INSTALL_PREFIX=/tmp让程序的安装目录变为/tmp/bin,默认情况CMAKE_INSTALL_PREFIX=/usr/local;
(2)若在CMakeLists.txt中设置了set(CMAKE_INSTALL_PREFIX /tmp/hello),此处的设置无效。
编译之后,hello目录中主要文件结构:
├── build │ ├── bin │ │ └── hello │ ├── lib │ │ ├── libhello.so │ │ └── libhello.a │ ├── libhello │ └── src ├── CMakeLists.txt ├── libhello │ ├── CMakeLists.txt │ ├── hello.c │ └── hello.h └── src ├── CMakeLists.txt └── main.c
build目录中有4个子目录,libhello和src目录中是源文件对应目录在cmake过程中生成的中间文件。
安装后,/tmp/hello目录内的结构:
├── bin │ └── hello ├── include │ └── hello.h └── lib ├── libhello.a └── libhello.so
参考资料:
http://blog.csdn.net/dbzhang800/article/details/6314073
http://blog.csdn.net/dbzhang800/article/details/6329068
http://www.cmake.org/Wiki/CMake_RPATH_handling
Cmake实践(by Cjacker)
2017年1月19日 22:41
可以试试 xmake。。也很方便的,用xmake.lua来描述工程。。http://www.xmake.io
target("test")
set_kind("binary")
add_files("src/*.c")
2024年8月03日 04:26
Thank you for this nice sharing.
2024年8月04日 20:01
This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are grea
2024年8月05日 14:12
Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
2024年8月05日 14:13
You have a real talent for writing unique content. I like how you think and the way you express your views in this article. I am impressed by your writing style a lot. Thanks for making my experience more beautiful.
2024年8月05日 14:15
Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I’ll be sure to check back again real soon.
2024年8月05日 14:17
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our.
2024年8月05日 14:18
Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!
2024年8月05日 14:18
Excellent article..ts a good site to explore ..Thanks for the blog post.Really thank you! Much obliged..
2024年8月05日 14:19
Great Post! Thanks for sharing such amazing information with us. Please keep sharing.
2024年8月05日 14:20
Nice to be visiting your blog again, it has been months for me. Well this article that i’ve been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share
2024年8月05日 14:21
Great Post! Thanks for sharing such amazing information with us. Please keep sharing.
2024年8月05日 14:22
Slot5000 adalah agen gacor deposit 5000 sudah bisa bermain di situs 303 slot 5000 login terpercaya yang sangat mudah menang dengan satu akun user id.
2024年8月05日 14:23
Excellent article..ts a good site to explore ..Thanks for the blog post.Really thank you! Much obliged..
2024年8月05日 14:23
Great Post! Thanks for sharing such amazing information with us. Please keep sharing.
2024年8月05日 14:24
Excellent article..ts a good site to explore ..Thanks for the blog post.Really thank you! Much obliged..
2024年8月05日 14:25
Your thoughtfulness and kindness have not gone unnoticed. I will remember it for the rest of my life.
2024年8月05日 14:25
You have a good point here!I totally agree with what you have said!!Thanks for sharing your views...hope more people will read this article!!
2024年8月05日 14:25
Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!
2024年8月05日 14:26
Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging
2024年8月05日 14:26
Excellent article..ts a good site to explore ..Thanks for the blog post.Really thank you! Much obliged..
2024年8月05日 14:27
I’m happy I located this blog! From time to time, students want to cognitive the keys of productive literary essays composing. Your first-class knowledge about this good post can become a proper basis for such people. nice one
2024年8月05日 14:29
Wow! This can be one particular I read more on the subject?
2024年8月05日 14:31
Excellent blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol
2024年8月05日 14:33
I’m happy I located this blog! From time to time, students want to cognitive the keys of productive literary essays composing. Your first-class knowledge about this good post can become a proper basis for such people. nice one
2024年8月05日 15:35
wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.
2024年8月05日 15:38
There are a lot of blogs over the Internet. But I can surely say that your blog is amazing in all. It has all the qualities that a perfect blog should have.
2024年8月05日 15:39
This website available for fulfill sexual desire of peoples who want to enjoy genuine services in Pune
2024年8月05日 15:41
Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks.
2024年8月05日 15:43
You have a real talent for writing unique content. I like how you think and the way you express your views in this article. I am impressed by your writing style a lot. Thanks for making my experience more beautiful.
2024年8月05日 15:45
This website available for fulfill sexual desire of peoples who want to enjoy genuine services in Pune