1

I'm trying to compile the following MWE that uses jsoncpp installed from Conan and set up with cmake, using g++-12 (from home-brew) but I'm clashing against all types of linking errors. (adding repo so its easier to run the MWE https://github.com/araml/link_error)

main.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

What I'm running

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

error output (partial)

[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)

If I set libc++, -lc++, I get a different type of error

-- 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

Compiling with apple-clang works just fine but I need to use g++-12 since clang doesn't support some of the features I want to use.

aram
  • 1,415
  • 13
  • 27
  • 2
    "I know not the best ..." - It is simply **wrong** to set `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER` variables **after** the `project()` call. See [that my answer](https://stackoverflow.com/a/63944545/3440745). – Tsyvarev Dec 30 '22 at 12:16
  • @Tsyvarev I'm already setting it in my script (see repo) that doesn't change or fix anything. – aram Dec 30 '22 at 12:33
  • GCC is not testes with Mac on ConanCenterIndex. Have built jsoncpp from sources? If yes, and if it works, please, share you full build log if possible. The Conan profile is important to match your settings and options, but is not listed. Also, have you tried with Clang, which the default compiler in Mac? – uilianries Dec 30 '22 at 12:50
  • @uilianries yes it works fine with apple-clang but I need to use gcc. I think my profile is the default one (Conan profile new --detect default) – aram Dec 30 '22 at 13:00
  • Since clang is a default compiler on your system, then probably you need to **explicitly** tell Conan to use gcc for build the required projects. Like in that question: https://stackoverflow.com/questions/74659157/how-to-use-gcc-with-conan-cmake-on-macos – Tsyvarev Dec 30 '22 at 14:25
  • @aram Please, share your full profile with `conan profile show default' than. Also, please, update your post with full build log if possible. Also, it's not clear where did you obtain that jsoncpp package, since Conan Center does not offer such configuration. Could you please clarify if you built that package? – uilianries Dec 30 '22 at 14:38
  • @Tsyvarev They are different questions. Your link points to question only related to Conan profile's configuration. In this current question, we can see `gcc` in the command line, which means, Aram knows how to configure its profile. – uilianries Dec 30 '22 at 14:52
  • @uilianries: I do not mean that referenced question is the same. I just mean that some information from that question can be useful here. "In this current question, we can see gcc in the command line" - Do you mean the command line "titled" with `[2/2] Linking CXX executable j1`? This command line is NOT from the Conan, when it builds `jsoncpp`. This command line is from CMake when it builds the consuming project (by means of Ninja tool). – Tsyvarev Dec 30 '22 at 16:34

1 Answers1

1

Maybe a lack of information in your profile. You have to be very explicit. Indeed on macOS, gcc & g++ are aliases for Apple clang. It works fine with this conan profile (I'm on macOS Intel, so I've tested with a different arch):

macos-armv8-gcc12 profile

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
SpacePotatoes
  • 659
  • 5
  • 11