Windows x86_64交叉编译log4cplus Conan x86包遇编译器缺失问题
解决Conan构建Windows 32位log4cplus时CMake找不到编译器的问题
问题根源
- 工具链引入不完整:当前
conanfile.py仅为x86_64架构添加mingw-builds工具链,32位构建时未引入对应工具链,导致CMake找不到编译器。 - 编译器版本不匹配:Profile中指定的GCC版本(7.3)与使用的
mingw-builds/12.2.0自带的GCC版本(12.2)不一致,Conan无法正确关联工具链。 - 旧版本log4cplus的CMake兼容性:log4cplus 1.1.3-rc3是较旧版本,可能需要额外配置CMake参数以适配MinGW 32位构建。
解决方案步骤
1. 修正conanfile.py的工具链依赖
修改build_requirements方法,根据Host架构自动引入对应版本的MinGW工具链,并指定架构选项:
def build_requirements(self): self.tool_requires("cmake/3.27.5") # 根据当前Host架构选择MinGW的arch选项 mingw_arch = "x86" if self.settings.arch == "x86" else "x86_64" self.tool_requires("mingw-builds/12.2.0", options={"arch": mingw_arch})
2. 更新Profile中的编译器版本
将两个Profile的compiler.version修改为mingw-builds/12.2.0对应的GCC版本12.2:
windows32debug:
[settings] os=Windows arch=x86 build_type=Debug compiler=gcc compiler.cppstd=11 compiler.version=12.2 compiler.threads=posix compiler.libcxx=libstdc++11
windows64debug:
[settings] os=Windows arch=x86_64 build_type=Debug compiler=gcc compiler.cppstd=11 compiler.version=12.2 compiler.threads=posix compiler.libcxx=libstdc++11
3. 为CMakeToolchain添加MinGW适配参数
在generate方法中添加针对旧版本log4cplus的MinGW构建配置,确保CMake能正确识别构建工具:
def generate(self): deps = CMakeDeps(self) deps.generate() tc = CMakeToolchain(self) if self.settings.os == "Windows" and self.settings.compiler == "gcc": # 指定MinGW对应的make程序 tc.variables["CMAKE_MAKE_PROGRAM"] = "mingw32-make" if self.settings.arch == "x86" else "mingw64-make" # 禁用CMake的编译器检查(部分旧版本log4cplus可能需要) tc.variables["CMAKE_C_COMPILER_WORKS"] = "TRUE" tc.variables["CMAKE_CXX_COMPILER_WORKS"] = "TRUE" tc.generate()
4. 重新执行构建命令
清理之前的构建缓存后,重新运行创建命令:
conan create conan\packages\log4cplus --profile:host=windows32debug --profile:build=windows64debug --build=missing
额外注意事项
- 若log4cplus 1.1.3-rc3的CMakeLists.txt存在32位构建的兼容性问题,可通过CMakeToolchain添加
-m32编译参数:tc.add_compile_options("-m32")。 - 确保
conancenter远程能正常拉取mingw-builds/12.2.0包,若存在权限问题,可检查remotes.json配置或手动上传对应包到私有Artifactory仓库。
内容的提问来源于stack exchange,提问作者Michel H
相关产品推荐
相关产品推荐

