[学习] windows 环境,创建静态、动态库

3 篇文章 0 订阅

[学习] windows 环境,创建静态、动态库

  1. 工具 visual studio 2019
  2. cmake

0. 准备文件

  1. main.cpp
#include <iostream>
#include "stu.h"

int main(){
	
	std::cout << "...in main.cpp...\n";
	
	stu s("张三");
	s.run();
	
	return 0;
	
}
  1. 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

  1. 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. 不链接任何库,构建项目

  1. CMakeLists.txt
cmake_minimum_required(VERSION 3.14)

project(TestDll)

set(CMAKE_CXX_STANDARD 14)

add_executable(
   test_dll
   main.cpp
   stu.cpp
)
  1. 项目结构
    在这里插入图片描述
  2. 执行结果
    在这里插入图片描述

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
)
  1. 项目结构
    在这里插入图片描述
  2. 执行结果(同上)

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
)
  1. 项目结构
    在这里插入图片描述

这里的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.

  1. 执行结果
    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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值