开发者

How to link jsoncpp?

How can I link jsoncpp with a C++ program using g++? I tried:

g++ -o program program.cpp -L/path/to/library/files -ljsoncpp, -ljson, -llibjsoncpp

but g++ keeps saying:

/usr/bin/ld: cannot find -lsomething
开发者_JS百科


You could also try using the new Amalgamated version of jsoncpp, which is new as of version 0.6.0.

The Amalgamated version allows you to use jsoncpp by adding just one directory with a couple of header files and one .cpp file to your project. You then can directly compile jsoncpp into your program with no worries about having to link to any jsoncpp libraries.


Look in /path/to/library/files to see what your *.a file is really named. On my system, I link with:

-ljson_linux-gcc-4.4.3_libmt

Some libraries will create a link from lib<name>.a to lib<name>-<version>.a for you, but I don't think that jsoncpp does this automatically. Therefore, you need to specify the complete name when linking.


You basically need to tell the path and the library.

jsoncpp library has pkg-config and you can use the command

`pkg-config --cflags path/to/jsoncpp/build/pkg-config/jsoncpp.pc`

for the include path and

`pkg-config --libs ../jsoncpp/build/pkg-config/jsoncpp.pc`

for linking (when running the command with g++ use the ). To see the single commands which is -L/libraryPath -ljsoncpp) run the commands above in the terminal without the ``.

It is easier to use this commands in a Makefile. As example my Makefile is:

CXX = g++

CXXFLAGS = -std=c++11
INC_PATH = `pkg-config --cflags ../jsoncpp/build/pkg-config/jsoncpp.pc`


LIBS = `pkg-config --libs ../jsoncpp/build/pkg-config/jsoncpp.pc`
SOURCES := $(wildcard *.cpp)
OBJDIR=obj

OBJECTS := $(patsubst %.cpp,$(OBJDIR)/%.o,$(SOURCES))
DEPENDS := $(patsubst %.cpp,$(OBJDIR)/%.d,$(SOURCES))

# ADD MORE WARNINGS!
WARNING := -Wall -Wextra

# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean

# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: yourProjectExecutableName

clean:
    $(RM) $(OBJECTS) $(DEPENDS) parking

# Linking the executable from the object files
yourProjectExecutableName: $(OBJECTS)
    $(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $@ $(LIBS)

-include $(DEPENDS)

$(OBJDIR):
    mkdir -p $(OBJDIR)

$(OBJDIR)/%.o: %.cpp Makefile $(OBJDIR)
    $(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) -MMD -MP -c $< -o $@

and then in the directory of the file I run make. The final command(s) will be printed out in the Terminal

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜