引入C++库

This commit is contained in:
2026-01-17 18:19:52 +08:00
parent 7f56f47f13
commit 0fa708c0de
8 changed files with 288 additions and 0 deletions

6
cpp/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 4.1)
project(cpp)
set(CMAKE_CXX_STANDARD 20)
add_library(cpp STATIC library.cpp)

9
cpp/library.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "library.h"
#include <iostream>
extern "C" {
void hello() {
std::cout << "Hello, World!" << std::endl;
}
}

7
cpp/library.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef CPP_LIBRARY_H
#define CPP_LIBRARY_H
extern "C" {
void hello();
}
#endif // CPP_LIBRARY_H

View File

@@ -0,0 +1,23 @@
# 目标系统 - 32位Windows
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR i686)
# 使用32位MinGW编译器
set(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
set(CMAKE_RC_COMPILER i686-w64-mingw32-windres)
set(CMAKE_AR i686-w64-mingw32-ar)
set(CMAKE_RANLIB i686-w64-mingw32-ranlib)
# 调整搜索路径
set(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# 静态链接运行时库避免需要额外DLL
set(CMAKE_EXE_LINKER_FLAGS_INIT "-static-libgcc -static-libstdc++ -static")
# 确保是32位
set(CMAKE_C_FLAGS_INIT "-m32")
set(CMAKE_CXX_FLAGS_INIT "-m32")