我在ubuntu上安装了jsoncpp和'sudo apt install libjsoncpp-dev‘,并试图运行一个极简的测试程序:
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>
int main()
{
ifstream file("example.json");
Json::Value value;
Json::Reader reader;
reader.parse(file,value);
cout << value << endl;
}我使用makefile来用以下标志编译我的代码:
CC=g++
CXXFLAGS=-std=c++17 -fopenmp -O3 -ljsoncpp发生下列错误:
/usr/bin/ld: /tmp/cc4MbV6f.o: in function `Test_JsonWriter()':
test.cpp:(.text+0x5865): undefined reference to `Json::Value::Value(Json::ValueType)'
/usr/bin/ld: test.cpp:(.text+0x5872): undefined reference to `Json::Reader::Reader()'
/usr/bin/ld: test.cpp:(.text+0x5885): undefined reference to `Json::Reader::parse(std::istream&, Json::Value&, bool)'
/usr/bin/ld: test.cpp:(.text+0x5894): undefined reference to `Json::operator<<(std::ostream&, Json::Value const&)'
/usr/bin/ld: test.cpp:(.text+0x5a9f): undefined reference to `Json::Value::~Value()'
/usr/bin/ld: /tmp/cc4MbV6f.o: in function `Test_JsonWriter() [clone .cold]':
test.cpp:(.text.unlikely+0x6bf): undefined reference to `Json::Value::~Value()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:21: test] Error 1我还确保更新所有软件包等等,还包括'#include ‘也没有帮助。
知道是怎么回事吗?谢谢你的帮助。
发布于 2022-03-09 09:56:54
user17732522的评论解决了我的问题!旗帜放置不当:
"-ljsoncpp必须放在编译器调用上的.cpp文件或.o文件的名称之后“-user17732522
https://stackoverflow.com/questions/71406883
复制相似问题