HelloWorld
HelloWorld ---> CMakeLists.txt
project(Hello)
add_subdirectory(src bin)
src ---> CMakeLists.txt
add_executable(hello main.c)
add_subdirectory用法:
add_subdirectory(source_dir [binary_dir] [exclude_from_all])
srouce_dir 源文件子目录
binary_dir 中间二进制和目标二进制存放位置
exclude_from_all 将这个目录从编译过程中排除
set(executable_output_path ${project_binary_dir}/bin)
set(library_output_path ${project_binary_dir}/lib)
构建动态库
add_library(libname [SHARED | STATIC | MODULE] [EXCLUDE_FROM_ALL] source1 source2 ... sourceN)
不需要写全libhello.so ,只需要填写hello即可,cmake系统会自动生成libhello.x
类型有三种:
SHARED 动态库
STATIC 静态库
module 在使用dyld的系统有效,如果不支持dyld,则当作shared对待
导入头文件
include_directories(/usr/include/hello)
添加共享库
link_directories 添加非标准库搜索路径
target_link_libraries 添加需要连接的共享库
例如:
target_link_libraries(main libhello.so)
target_link_libraries(main libhello.a)