目录
[学习] windows 环境,创建静态、动态库
- 工具 visual studio 2019
- cmake
0. 准备文件
- main.cpp
#include <iostream>
#include "stu.h"
int main(){
std::cout << "...in main.cpp...\n";
stu s("张三");
s.run();
return 0;
}
- stu.h
#ifndef DAY13_STU_H
#define DAY13_STU_H
#include <iostream>
using namespace std;
class stu{
public:
string name;
stu();
stu(string name);
void run();
~stu();
};
#endif
- stu.cpp
#include "stu.h"
#include <iostream>
using namespace std;
stu::stu(){
cout <<"无参构造" <<endl;
}
stu::stu(string name):name(name){
cout <<"有参构造" <<endl;
}
void stu::run(){
cout << name << " 在公园里跑步!" << endl;
}
stu::~stu(){
cout <<"析构" <<endl;
}
1. 不链接任何库,构建项目
- CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(TestDll)
set(CMAKE_CXX_STANDARD 14)
add_executable(
test_dll
main.cpp
stu.cpp
)
- 项目结构
- 执行结果
2. 生成静态库
CMakeLists.txt
生成的.lib
文件在\build\debug\
下
cmake_minimum_required(VERSION 3.14)
project(TestDll)
set(CMAKE_CXX_STANDARD 14)
add_executable(
test_dll
main.cpp
#stu.cpp # 编译成静态库
)
add_library(
stu_static
STATIC
stu.cpp
)
# 让程序关联动态库
target_link_libraries( #如果不链接静态库,会报错: LNK2019,unresolved external symbol
test_dll
stu_static
)
- 项目结构
- 执行结果(同上)
3. 生成动态库(同时生成动态库,同名)
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(TestDll)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
add_library(stu
SHARED
stu.cpp
stu.h
)
set_target_properties(
stu
PROPERTIES
POSITION_INDEPENDENT_CODE 1
)
# add_library( # 不需要使用这个命令生成静态库
# stu_static
# static
# stu.cpp
# )
add_executable(
test_dll
main.cpp
#stu.cpp
)
set_target_properties(
test_dll
PROPERTIES
POSITION_INDEPENDENT_CODE 1
)
# 让程序关联动态库
#message(STATUS "PROJECT_SOURCE_DIR -- " ${PROJECT_SOURCE_DIR})
target_link_libraries( #如果不链接静态库,会报错: LNK2019,unresolved external symbol
test_dll
stu
)
- 项目结构
这里的
stu
是动态库.dll
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
这个命令可以同时生成.lib
与.dll
。
This property is implemented only for MS-compatible tools on Windows.
Enable this boolean property to automatically create a module definition (.def) file with all global symbols found in the input .obj files for a SHARED library (or executable with ENABLE_EXPORTS) on Windows. The module definition file will be passed to the linker causing all symbols to be exported from the .dll. For global data symbols, __declspec(dllimport) must still be used when compiling against the code in the .dll. All other function symbols will be automatically exported and imported by callers. This simplifies porting projects to Windows by reducing the need for explicit dllexport markup, even in C++ classes.
When this property is enabled, zero or more .def files may also be specified as source files of the target. The exports named by these files will be merged with those detected from the object files to generate a single module definition file to be passed to the linker. This can be used to export symbols from a .dll that are not in any of its object files but are added by the linker from dependencies (e.g. msvcrt.lib).
This property is initialized by the value of the CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS variable if it is set when a target is created.
- 执行结果
Debug目录
Release目录
运行结果
同上
4. 生成动态库+链接动态库(TODO)
CMakeLists.txt
5. 生成同名静态+动态库(TODO)
CMakeLists.txt
6. [未解决] LNK1112 module machine type ‘x86’ conflicts with target machine type ‘x64’
链接: Solving Link error 1112.
链接: fatal error LNK1112: module machine type ‘x64’ conflicts with target machine type ‘X86’.
参考
链接: CMake Cookbook-2.2.
链接: WINDOWS_EXPORT_ALL_SYMBOLS.
链接: (windows平台下)深入详解C++创建动态链接库DLL以及如何使用它(一).
链接: VS2017的动态链接库(Dynamic Link Library)配置.
链接: Create C Program with Static Library using Command Line in Windows.
链接: Adding the Visual Studio Developer Command Prompt to Windows Terminal.
链接: linux编译器的下关于gcc、g++、make和CMake几个概念的区别.
链接: link.
链接: link.
链接: link.
链接: link.
链接: link.