定义MinGW编译器的包含路径

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

Defining include path for mingw compiler

问题

我在尝试在Linux操作系统上使用Mingw编译器进行交叉编译到Windows二进制文件。

在这个非常简单的项目中,我使用了一个包含:

#include <nlohmann/json.hpp>

json.hpp 位于 /usr/include/nlohmann
当我通过g++编译项目时,编译器会查找其默认的包含路径,并能够找到头文件,没有问题。

然而,当我通过mingw编译项目时,它使用不同的包含路径,因此我必须明确指定包含路径。我使用这个命令:

x86_64-w64-mingw32-g++ -I/usr/include/nlohmann --static main.cpp 

但我得到了这个错误:

fatal error: nlohmann/json.hpp: No such file or directory
    2 | #include <nlohmann/json.hpp>

有谁能帮我吗?注意:json.hpp 文件依赖于nlohmann文件夹中的其他文件,因此仅包括json.hpp文件是行不通的。

英文:

I'm trying to use Mingw compiler on Linux OS for cross-compiling to Windows binaries.

In the very simple project I use one include:

#include <nlohmann/json.hpp>

The json.hpp is located in /usr/include/nlohmann.
When I compile the project via g++, the compiler looks for its default include paths and is able to find the header file without problems.

However, when I compile the project via mingw, it uses different include paths, so I have to specify the include path explicitly. I use this command:

x86_64-w64-mingw32-g++ -I/usr/include/nlohmann --static main.cpp 

But I get this error:

fatal error: nlohmann/json.hpp: No such file or directory
    2 | #include <nlohmann/json.hpp>

Can anybody help me please?
NOTE: the json.hpp file depends on other files from nlohmann folder, so including only the json.hpp file will not work.

答案1

得分: 0

MinGW 不搜索 /usr/include 其实是一件好事。那里的库可能与 MinGW 不兼容。

现在,nlohmann/json 本身是一个只包含头文件且没有平台特定生成头文件的库,所以它可能可以直接使用。但是尝试将 -I/usr/include 添加到编译标志中(以便让编译器找到该文件)会引发巨大问题,因为那里的所有其他头文件可能也会被使用。

相反,你应该自己克隆 nlohmann/json,然后将仓库中的 include/ 目录添加到搜索路径中(使用 -I)。

此外,还有 quasi-msys2,它为 MinGW 库提供了一个包管理器。不过对于不需要编译的头文件库来说,使用它的好处可能不太大(不需要手动克隆仓库和设置包含路径)。(我是作者。)

英文:

MinGW not searching /usr/include is actually a good thing. Libraries in there might not work with MinGW.

Now, nlohmann/json itself, being header-only and without any platform-specific generated headers, might work as is. But trying to add <s>-I/usr/include</s> to flags (which would let the compiler find the file) would cause huge problems, as all the other headers in there might get used as well.

Instead, what you should do is to clone nlohmann/json yourself, and add the include/ directory in the repo to the search path (with -I).


Also there's quasi-msys2, which has a package manager for MinGW libraries. Though for header-only libraries that don't need to be compiled, the benefit of using it is rather small (not having to manually clone the repo and set the include path). (I'm the author.)

答案2

得分: 0

If the file is located at /usr/include/nlohmann/json.hpp and the include line is:

#include &lt;nlohmann/json.hpp&gt;

what you need is -I/usr/include.

Then it will find nlohmann/json.hpp under /usr/include.

Note that it's only okay to do this if this is a header-only library that is not different for the specific target platform you are cross-compiling to.

In general, you need to have dependency libraries compiled for the same target platform and link with them. That's also the reason the cross-compiler won't look at the stuff in /usr/include.

英文:

If the file is located at /usr/include/nlohmann/json.hpp and the include line is

#include &lt;nlohmann/json.hpp&gt;

what you need is -I/usr/include.

Then it will find nlohmann/json.hpp under /usr/include.

Note that it's only okay to do this if this is a header-only library that is not different for the specific target platform you are cross-compiling to.

In general you need to have dependency libraries compiled for the same target platform and link with them. That's also the reason the cross compiler won't look at the stuff in /usr/include.

huangapple
  • 本文由 发表于 2023年4月19日 23:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056346.html
匿名

发表评论

匿名网友

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

确定