编译错误 g++ + cmake + Conan + macOS m2 + jsoncpp

问题描述 投票:0回答:2

我正在尝试编译以下 MWE,它使用从 Conan 安装的 jsoncpp 并使用 cmake 设置,使用 g++-12 (来自 home-brew),但我与所有类型的链接错误发生冲突。 (添加存储库以便更轻松地运行 MWE https://github.com/araml/link_error

主.cpp。

#include <cstdio>
#include <fstream>
#include <json/json.h>

int main() { 
    std::ifstream j1_json("j1");
    
    Json::Value j1;
    Json::Reader parser;

    if (!parser.parse(j1_json, j1)) { 
        puts("Error when parsing j1");
    }

    return 0;
}

cmake

cmake_minimum_required(VERSION 3.16)

project(j1_test)
enable_testing()

set(CMAKE_CXX_FLAGS "-std=c++2a -ggdb -Wall -Wextra -Og -fno-inline-functions") 
set(CMAKE_C_COMPILER "gcc-12") # I know not the best but easier here for the examples purpose
set(CMAKE_CXX_COMPILER "g++-12")

find_package(jsoncpp CONFIG REQUIRED)

add_executable(j1 src/main.cpp)
target_link_libraries(j1 JsonCpp::JsonCpp)

conanfile.txt

[requires]
     jsoncpp/1.9.5

[generators]
     CMakeDeps
     CMakeToolchain

我在跑什么

conan install conanfile.txt --install-folder conan --build=missing -pr:b=default
mkdir -p build 
push build
cmake -DCMAKE_TOOLCHAIN_FILE=./conan/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release -G Ninja ../
cmake --build . -j 8
popd

错误输出(部分)

[2/2] Linking CXX executable j1
FAILED: j1
: && g++-12 -std=c++2a -ggdb -Wall -Wextra -Og -fno-inline-functions -O3 -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/j1.dir/src/main.cpp.o -o j1  /Users/meera/.conan/data/jsoncpp/1.9.5/_/_/package/2f2de4e3345f667bb03ed16a03f45c72c978d397/lib/libjsoncpp.a && :
Undefined symbols for architecture arm64:
  "__ZN4Json6Reader5parseERSiRNS_5ValueEb", referenced from:
      _main in main.cpp.o
  "__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4findEcm", referenced from:
      __ZN4Json12_GLOBAL__N_113valueToStringEdbjNS_13PrecisionTypeE in libjsoncpp.a(json_writer.cpp.o)
  "__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7compareEmmPKcm", referenced from:
      __ZNK4Json19StreamWriterBuilder15newStreamWriterEv in libjsoncpp.a(json_writer.cpp.o)
  "__ZNKSt3__115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEE3strEv", referenced from:
      __ZN4Json5ValueC2EPKc in libjsoncpp.a(json_value.cpp.o)
      __ZN4JsonL29duplicateAndPrefixStringValueEPKcj in libjsoncpp.a(json_value.cpp.o)
      __ZNK4Json5Value9asCStringEv in libjsoncpp.a(json_value.cpp.o)
      __ZNK4Json5Value8asStringEv in libjsoncpp.a(json_value.cpp.o)
      __ZNK4Json5Value5asIntEv in libjsoncpp.a(json_value.cpp.o)
      __ZNK4Json5Value6asUIntEv in libjsoncpp.a(json_value.cpp.o)
      __ZNK4Json5Value7asInt64Ev in libjsoncpp.a(json_value.cpp.o)

如果我设置 libc++,

-lc++
,我会收到不同类型的错误

-- Using Conan toolchain: jsoncpp_test/conan/conan_toolchain.cmake
-- Conan: Target declared 'JsonCpp::JsonCpp'
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/meera/Projects/jsoncpp_test/build
[2/2] Linking CXX executable j1
FAILED: j1
: && g++-12 -std=c++2a -ggdb -Wall -Wextra -lc++ -Og -fno-inline-functions -O3 -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/j1.dir/src/main.cpp.o -o j1  /Users/meera/.conan/data/jsoncpp/1.9.5/_/_/package/2f2de4e3345f667bb03ed16a03f45c72c978d397/lib/libjsoncpp.a && :
Undefined symbols for architecture arm64:
  "__ZN4Json6Reader5parseERSiRNS_5ValueEb", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
~/Projects/jsoncpp_test

使用 apple-clang 编译效果很好,但我需要使用 g++-12,因为 clang 不支持我想使用的一些功能。

c++ cmake arm64 conan jsoncpp
2个回答
1
投票

您的个人资料中可能缺少信息。你必须非常明确。事实上,在 macOS 上,gcc 和 g++ 是 Apple clang 的别名。 它与这个 conan 配置文件配合得很好(我在 macOS Intel 上,所以我用不同的架构进行了测试):

macos-armv8-gcc12 配置文件

cc=/usr/local/bin/gcc-12
cxx=/usr/local/bin/g++-12
ar=/usr/local/bin/gcc-ar-12
nm=/usr/local/bin/gcc-nm-12
ranlib=/usr/local/bin/gcc-ranlib-12

[settings]
os=Macos
arch=armv8
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++11
build_type=Release
[options]
[tool_requires]
[conf]
tools.apple:sdk_path=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
tools.build:compiler_executables={"c": "$cc", "cpp": "$cxx"}
[buildenv]
CC=$cc
CXX=$cxx
AR=$ar
NM=$nm
RANLIB=$ranlib

# legacy
[env]
CC=$cc
CXX=$cxx
AR=$ar
NM=$nm
RANLIB=$ranlib

0
投票

我通过创建柯南配置文件修复了这个问题

conan profile new --detect build       

改变

compiler=apple-clang
compiler.version=15

进入

compiler=gcc
compiler.version=12

                                                                                                                     

然后添加

[conf]
tools.build:compiler_executables={"c": "gcc-12", "cpp": "g++-12"}

终于可以运行柯南了

-pr=build

因此它会选择配置文件并使用 g++ 编译包。

© www.soinside.com 2019 - 2024. All rights reserved.