You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Makefile链接jsoncpp库时的未定义引用错误?

解决jsoncpp链接时的未定义引用错误

我在项目中使用jsoncpp库,尝试通过Makefile接入时遇到未定义引用的链接错误。项目结构如下:

  • client.json
  • cross-build-make.sh
  • main.cpp
  • main.hpp
  • Makefile

尝试的两种Makefile配置及错误

第一种:使用pkg-config自动获取路径

CXX=g++
CXXFLAGS=-std=c++11 -Wall -Wextra -pedantic

JSONCPP_INCLUDE := $(shell pkg-config --cflags jsoncpp)
JSONCPP_LIB := $(shell pkg-config --libs jsoncpp)

TARGET=my_program

SRCS=main.cpp

OBJS=$(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CXX) $(CXXFLAGS) $(JSONCPP_LIB) $(OBJS) -o $(TARGET)

%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(JSONCPP_INCLUDE) -c $< -o $@

clean:
    rm -f $(OBJS) $(TARGET)

编译错误输出:

g++ -std=c++11 -Wall -Wextra -pedantic -I/usr/include/jsoncpp -c main.cpp -o main.o
g++ -std=c++11 -Wall -Wextra -pedantic -ljsoncpp main.o -o my_program
/usr/bin/ld: main.o: in function «main»:
main.cpp:(.text+0x2f): undefined reference to «Json::Value::Value(Json::ValueType)»
/usr/bin/ld: main.cpp:(.text+0x63): undefined reference to «Json::operator>>(std::istream&, Json::Value&)»
/usr/bin/ld: main.cpp:(.text+0x79): undefined reference to «Json::Value::operator[](char const*)»
/usr/bin/ld: main.cpp:(.text+0x8e): undefined reference to «Json::Value::asString[abi:cxx11]() const»
/usr/bin/ld: main.cpp:(.text+0xa4): undefined reference to «Json::Value::operator[](char const*)»
/usr/bin/ld: main.cpp:(.text+0xb9): undefined reference to «Json::Value::asString[abi:cxx11]() const»
/usr/bin/ld: main.cpp:(.text+0x168): undefined reference to «Json::Value::~Value()»
collect2: error: ld returned 1 exit status
make: *** [Makefile:25: my_program] Error 1
rm -f main.o my_program

第二种:手动指定库路径

CXX=g++
CXXFLAGS=-std=c++11 -Wall -Wextra -pedantic

JSONCPP_INCLUDE=-I/usr/include/

JSONCPP_LIB=-L/usr/lib/x86_64-linux-gnu -ljsoncpp

TARGET=my_program

SRCS=main.cpp

OBJS=$(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CXX) $(CXXFLAGS) $(JSONCPP_LIB) $(OBJS) -o $(TARGET)

%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(JSONCPP_INCLUDE) -c $< -o $@

clean:
    rm -f $(OBJS) $(TARGET)

编译错误输出:

g++ -std=c++11 -Wall -Wextra -pedantic -I/usr/include/ -c main.cpp -o main.o
g++ -std=c++11 -Wall -Wextra -pedantic -L/usr/lib/x86_64-linux-gnu -ljsoncpp 
/usr/bin/ld: main.o: in function «main»:
main.cpp:(.text+0x2f): undefined reference to «Json::Value::Value(Json::ValueType)»
/usr/bin/ld: main.cpp:(.text+0x63): undefined reference to «Json::operator>>(std::istream&, Json::Value&)»
/usr/bin/ld: main.cpp:(.text+0x79): undefined reference to «Json::Value::operator[](char const*)»
/usr/bin/ld: main.cpp:(.text+0x8e): undefined reference to «Json::Value::asString[abi:cxx11]() const»
/usr/bin/ld: main.cpp:(.text+0xa4): undefined reference to «Json::Value::operator[](char const*)»
/usr/bin/ld: main.cpp:(.text+0xb9): undefined reference to «Json::Value::asString[abi:cxx11]() const»
/usr/bin/ld: main.cpp:(.text+0x168): undefined reference to «Json::Value::~Value()»
collect2: error: ld returned 1 exit status
make: *** [Makefile:25: my_program] Error 1
rm -f main.o my_program

问题原因与解决方法

核心原因

GCC链接器的参数顺序是关键:依赖库(-lxxx)必须放在需要引用它的目标文件之后。你之前的配置中,JSONCPP_LIB在OBJS前面,导致链接器处理库时还未读取到需要依赖这些库的目标文件符号,从而无法解析引用。

修正后的Makefile

版本一(pkg-config)

CXX=g++
CXXFLAGS=-std=c++11 -Wall -Wextra -pedantic

JSONCPP_INCLUDE := $(shell pkg-config --cflags jsoncpp)
JSONCPP_LIB := $(shell pkg-config --libs jsoncpp)

TARGET=my_program

SRCS=main.cpp

OBJS=$(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CXX) $(CXXFLAGS) $(OBJS) $(JSONCPP_LIB) -o $(TARGET)  # 库移至目标文件后

%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(JSONCPP_INCLUDE) -c $< -o $@

clean:
    rm -f $(OBJS) $(TARGET)

版本二(手动指定路径)

CXX=g++
CXXFLAGS=-std=c++11 -Wall -Wextra -pedantic

JSONCPP_INCLUDE=-I/usr/include/

JSONCPP_LIB=-L/usr/lib/x86_64-linux-gnu -ljsoncpp

TARGET=my_program

SRCS=main.cpp

OBJS=$(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CXX) $(CXXFLAGS) $(OBJS) $(JSONCPP_LIB) -o $(TARGET)  # 库移至目标文件后

%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(JSONCPP_INCLUDE) -c $< -o $@

clean:
    rm -f $(OBJS) $(TARGET)

额外检查项

  1. 确认系统已正确安装libjsoncpp-dev包(通过包管理器安装)
  2. 代码中头文件引用正确,应为#include <json/json.h>(而非jsoncpp/json.h,除非自定义了路径)

内容的提问来源于stack exchange,提问作者Fedor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 05:02:13