Fatal error with jsoncpp while compiling for Darwin

huangapple go评论74阅读模式
英文:

Fatal error with jsoncpp while compiling for Darwin

问题

I am facing the exact same error as this but for mac os

Error message looks like this:

myfile.cpp:12:10: fatal error: json/json.h: No such file or directory

initially I was trying to run it like this:

g++-11 myfile.cpp -o myfile -ljsoncpp

then I changed it to this:

g++-11 -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I/usr/local/opt/jsoncpp/include -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11  -I./json -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I/path/to/current/dir/json -L/usr/local/opt/jsoncpp pingpong.cpp -o pingpong -ljsoncpp

myfile.cpp was like this intitally:

#include <json/json.h>
...

then influenced by this post I changed it to this:

#include <jsoncpp/json/json.h>
...

but no matter what I do I seem to get the same error

This:

find /usr -name json.h | grep -f json/

Doesnt return anything.

PS: The path of jsoncpp that I used is what I got from this:

brew info jsoncpp
==> jsoncpp: stable 1.9.5, HEAD
Library for interacting with JSON
https://github.com/open-source-parsers/jsoncpp
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/jsoncpp.rb
License: MIT
==> Dependencies
Build: meson ✘, ninja ✘
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 4,398 (30 days), 672 (90 days), 66,483 (365 days)
install-on-request: 2,808 (30 days), 86 (90 days), 3,736 (365 days)
build-error: 0 (30 days)
brew --prefix jsoncpp
/usr/local/opt/jsoncpp
英文:

I am facing the exact same error as this but for mac os

Error message looks like this:

myfile.cpp:12:10: fatal error: json/json.h: No such file or directory

initially I was trying to run it like this:

g++-11 myfile.cpp -o myfile -ljsoncpp

then I changed it to this:

g++-11 -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I/usr/local/opt/jsoncpp/include -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I./json -L/usr/local/opt/jsoncpp myfile.cpp -o myfile -ljsoncpp

and then to this:

g++-11 -I/path/to/current/dir/json -L/usr/local/opt/jsoncpp pingpong.cpp -o pingpong -ljsoncpp

myfile.cpp was like this intitally:

#include <json/json.h>
...

then influenced by this post I changed it to this:

#include <jsoncpp/json/json.h>
...

but no matter what I do I seem to get the same error

This:

find /usr -name json.h | grep -f json/
Doesnt return anything.

PS: The path of jsoncpp that I used is what I got from this:

brew info jsoncpp

==> jsoncpp: stable 1.9.5, HEAD
Library for interacting with JSON
https://github.com/open-source-parsers/jsoncpp
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/jsoncpp.rb
License: MIT
==> Dependencies
Build: meson ✘, ninja ✘
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 4,398 (30 days), 672 (90 days), 66,483 (365 days)
install-on-request: 2,808 (30 days), 86 (90 days), 3,736 (365 days)
build-error: 0 (30 days)

brew --prefix jsoncpp

/usr/local/opt/jsoncpp

答案1

得分: 1

-I/opt/homebrew/include#include <json/json.h> 的包含目录。

-L/opt/homebrew/lib-ljsoncpp 的库目录。

这些包含和库路径对于大多数 Homebrew 包都是有效的。

/opt/homebrew/opt/jsoncpp 是安装目录。

您可以安装 brew install pkg-config。然后可以检索编译器和链接器标志:

$ pkg-config jsoncpp --cflags --libs
-I/opt/homebrew/Cellar/jsoncpp/1.9.5/include -L/opt/homebrew/Cellar/jsoncpp/1.9.5/lib -ljsoncpp

以及构建命令:

$ g++-11 myfile.cpp -o myfile `pkg-config jsoncpp --cflags --libs`
英文:

-I/opt/homebrew/include is the include directory for #include <json/json.h>.

-L/opt/homebrew/lib is the library directory for -ljsoncpp.

The include and the library paths are valid for the most of homebrew packages.

/opt/homebrew/opt/jsoncpp is the install directory.

You could install brew install pkg-config. Then the compiler and the linker flags can be retrieved:

$ pkg-config jsoncpp --cflags --libs
-I/opt/homebrew/Cellar/jsoncpp/1.9.5/include -L/opt/homebrew/Cellar/jsoncpp/1.9.5/lib -ljsoncpp

And the build command:

$ g++-11 myfile.cpp -o myfile `pkg-config jsoncpp --cflags --libs`

答案2

得分: 0

正如@RetiredNinja指出的那样,将#include <json/json.h>转换为#include "json/json.h"就解决了问题。

英文:

As @RetiredNinja pointed out converting #include <json/json.h> to #include "json/json.h" did the trick.

huangapple
  • 本文由 发表于 2023年6月8日 14:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定