涉及到三个输出路径的指定: 库输出路径、归档输出路径、执行程序输出路径。
库输出路径: CMAKE_LIBRARY_OUTPUT_DIRECTORY 只能输出linux 动态库 或者mac 动态库的路径
归档输出路径: CMAKE_ARCHIVE_OUTPUT_DIRECTORY , windows 静态库lib, windows动态库地址 .lib 文件 , Linux静态库 .a
执行程序输出路径: CMAKE_RUNTIME_OUTPUT_DIRECTORY 执行程序和dll 动态库
test_xlog/CMakeLists.txt
#CMakeLists.txt test_xlog 102
cmake_minimum_required(VERSION 3.20)
project(test_xlog)
#指定头文件查找路径
include_directories("../xlog")
# 指定库查找路径 window自动找 ../xlog/build/Debug ../xlog/build/Release
link_directories("../xlog/build")
add_executable(test_xlog test_xlog.cpp)
# 指定加载的库
target_link_libraries(test_xlog xlog)
test_xlog/test_xlog.cpp
#include <iostream>
#include "xlog.h"
using namespace std;
int main()
{
XLog log;
cout<<"test xlog"<<endl;
return 0;
}
xlog/CMakeLists.txt
#106cmake_system_ver/CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(xlog)
#1 提供信息的变量 项目名称 ${PROJECT_NAME}对应project的name
#2 改变行为的变量 BUILD_SHARED_LIBS ON 动态库 OFF静态库 默认OFF
#set(BUILD_SHARED_LIBS ON)
set(BUILD_SHARED_LIBS OFF) #静态库
#cmake传递变量给c++
add_definitions(-Dxlog_STATIC) #默认值1
#3 描述系统的变量
message("MSVC = " ${MSVC})
message("WIN32 = " ${WIN32})
message("UNIX = " ${UNIX})
message("CMAKE_SYSTEM_NAME = " ${CMAKE_SYSTEM_NAME})
# 4 控制构建过程的变量 输出路径控制 CMAKE_COLOR_MAKEFILE 是否生成makefile的颜色,默认是ON
set(CMAKE_COLOR_MAKEFILE OFF)
add_library(${PROJECT_NAME} xlog.cpp xlog.h)
xlog.cpp
#include "xlog.h"
#include <iostream>
using namespace std;
XLog::XLog()
{
cout<<"Create XLog"<<endl;
}
xlog.h
//xlog.h
#ifndef XLOG_H
#define XLOG_H
//__declspec(dllexport)
//__declspec(dllexport) 导出XLog类的函数到lib文件中
// xlog库文件调用 dllexport
// test_xlog 调用 dllimport
#ifndef _WIN32 //linux mac unix Android
#define XCPP_API
#else //windows
#ifdef xlog_STATIC //静态库
#define XCPP_API
#else //动态库
#ifdef xlog_EXPORTS
#define XCPP_API __declspec(dllexport) //库项目调用
#else
#define XCPP_API __declspec(dllimport) //调用库项目调用
#endif
#endif
#endif
class XCPP_API XLog
{
public:
XLog();
};
#endif
根目录的CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(xlog)
include_directories("xlog")
#CMakeLists.txt路径
message("CMAKE_CURRENT_LIST_DIR = ${CMAKE_CURRENT_LIST_DIR}")
# .so库输出路径 默认路径在-B build目录下
# 当前路径 下的 lib/
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../../lib")
#linux .so输出路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/lib")
#执行程序和dll 动态库pdb调试文件 输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/bin")
#归档输出路径 静态库 .lib 动态库.lib地址文件 linux静态库.a 静态库pdb调试文件
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/lib")
#添加xlog库编译 项目自带预处理变量 xlog_EXPORTS
#add_library(xlog SHARED xlog/xlog.cpp)
set(BUILD_SHARED_LIBS OFF) #静态库
#cmake传递变量给c++
add_definitions(-Dxlog_STATIC) #默认值1
add_library(xlog xlog/xlog.cpp)
# add_library(xlog_d SHARED xlog/xlog.cpp)
#执行文件
add_executable(test_xlog test_xlog/test_xlog.cpp)
#链接库
target_link_libraries(test_xlog xlog)
cmake add_subdirectory
cmake_minimum_required(VERSION 3.20)
project(xlog)
include_directories("xlog")
#CMakeLists.txt路径
message("CMAKE_CURRENT_LIST_DIR = ${CMAKE_CURRENT_LIST_DIR}")
# .so库输出路径 默认路径在-B build目录下
# 当前路径 src/109 lib/
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../../lib")
#linux .so输出路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/lib")
#执行程序和dll 动态库pdb调试文件 输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/bin")
#归档输出路径 静态库 .lib 动态库.lib地址文件 linux静态库.a 静态库pdb调试文件
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/lib")
#添加xlog库编译 项目自带预处理变量 xlog_EXPORTS
#add_library(xlog SHARED xlog/xlog.cpp)
#set(BUILD_SHARED_LIBS OFF) #静态库
#cmake传递变量给c++
#add_definitions(-Dxlog_STATIC) #默认值1
set(BUILD_SHARED_LIBS ON) #动态库
add_subdirectory("xlog")
add_subdirectory("test_xlog")