利用cmake 编译静态库并完成install

该文章展示了如何使用Conan进行依赖管理,特别是在Windows环境下配置VisualStudio编译器和CMake。它指定了版本为16的VisualStudio,并引入了libcurl库作为依赖。同时,文件包含了复制头文件和库到指定目录的指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

conan dependency

# -*- coding: utf-8 -*
from conans import ConanFile, CMake, tools, load
import re

class testConan(ConanFile):
    name = "test"
    version = "1.0.0"
    author = "xxx"
    description = "conan test"
    settings = "os", "compiler", "build_type", "arch"
    generators = "cmake", "xcode", "visual_studio"

    def configure(self):
        if self.settings.os == "Windows":
            self.settings.compiler = "Visual Studio"
            self.settings.compiler.version = 16
        else:
            raise Exception("Error: not support os!")        

    def imports(self):
        if self.settings.os == "Windows":
            self.copy("*.lib", "./lib", "lib")
            self.copy("*.pdb", "./lib", "lib")
            self.copy("*.winmd", "./lib", "lib")
            self.copy("*gen/*.hpp", "./include", "include", "common")
            self.copy("*.hpp", "./include", "include", "utility")
            self.copy("*.h", "./include", "include", "curl")
            self.copy("*.h", "./include", "include", "rapidjson")
            self.copy("*.h", "./include", "include", "crashDump")
            self.copy("*.h", "./include", "include", "lib7z")
            self.copy("*.h", "./include", "include", "quill")
            self.copy("*.h", "./include/pthread", "include", "pthread")
            self.copy("*.hpp", "./include", "include", "mio")
            self.copy("*.*", "./include", "include", "odb")
            self.copy("*.dll", "./bin", "bin")
            self.copy("*WebView*.exe", "./bin", "bin")
            self.copy("webrtc_shared.dll.pdb", "./bin", "pdb")
    
    def requirements(self):
        if self.settings.os == "Windows":
                self.requires("libcurl/7.87.0") 
        else:
            raise Exception("Error: not support os!")

    def package(self):
        self.copy("*", dst="include", src="./out/include/", keep_path=True)
        self.copy("*", dst="lib", src="./out/lib/", keep_path=True)
        self.copy("*", dst="bin", src="./out/bin/", keep_path=True)

    def package_info(self):
        self.cpp_info.libs = tools.collect_libs(self)

cmake

cmake_minimum_required(VERSION 3.6.0)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/output) # 设置install 时候的路径
project(testProject LANGUAGES CXX)

# conan config
include(./archtectures/conanbuildinfo.cmake)
conan_basic_setup(TARGETS NO_OUTPUT_DIRS)
include_directories(${CONAN_INCLUDE_DIRS})
include_directories(${CONAN_INCLUDE_DIRS_UTILITY}/optional)
include_directories(${CONAN_INCLUDE_DIRS_UTILITY}/stringify)
include_directories(${CONAN_INCLUDE_DIRS_UTILITY}/json)
link_directories(${CONAN_LIB_DIRS})

# output pdb
set(CMAKE_CXX_FLAGS_RELEASE "/MD /Zi /Od /DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "/MDd /Zi /Od")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

message("CMAKE_INSTALL_PREFIX= ${CMAKE_INSTALL_PREFIX}")

# For build target
file (GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.c??)
add_library(testProject STATIC ${SRC_FILES})

# insatll
SET(targets)
SET(targets ${targets} testProject)
INSTALL(TARGETS ${targets}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)

INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ 
        DESTINATION include/ 
        FILES_MATCHING PATTERN *.h**)

file(GLOB_RECURSE PDB_FILES ${PROJECT_BINARY_DIR} 
     FILES_MATCHING PATTERN *.pdb)

INSTALL(FILES ${PDB_FILES} 
        DESTINATION lib)

build script

conan install . -s os=Windows -s build_type=${build_type} -r conancenter --update --install-folder archtectures
cmake -Bbuild -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=${build_type}
cmake --build build --target install --config ${build_type}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值