编译时报错现象:
undefined reference to `Json::Value::Value
根据这个链接的说法,you have jsoncpp compiled with a different C++ ABI than you link against. The easiest solution is to use exactly the same compiler-version to build both library and executable. jsoncpp使用了其他的c++ ABI编译,而不是现在正在使用的c++ ABI 版本。
查看cmake的输出,发现cmake使用的是另外一个c++,而不是默认的c++。也就是cmake没有找到正确的c++版本,原因是(链接):cmake默认使用cc而不是gcc,如果默认的c++路径中没有cc的话,cmake就会去找其他的路径,找到后,c++也会使用这个路径下面的,从而导致默认的c++没有被使用,解决方法是制定cmake使用的cc为gcc,见下面的参数
essentially CMake uses cc
and c++
wherever they are off the $PATH
, which is not always the same as gcc
or g++
.
Specifying these compilers fixes the version shown by CMake:
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ ..