简体   繁体   English

在 mac 上,g++ (clang) 默认搜索 /usr/local/include 和 /usr/local/lib 失败

[英]On mac, g++ (clang) fails to search /usr/local/include and /usr/local/lib by default

I'm on a mac and I used homebrew to install gmp .我在 mac 上,我使用homebrew安装gmp

Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx
In file included from main.cpp:2:
./gcjlib.hpp:4:10: fatal error: 'gmpxx.h' file not found
#include <gmpxx.h>
         ^
1 error generated.

So then I explicitly told g++ to use /usr/local/include所以然后我明确告诉g++使用/usr/local/include

Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include
ld: library not found for -lgmp
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So then I explicitly told g++ to use /usr/local/lib所以我明确告诉g++使用/usr/local/lib

Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include -L/usr/local/lib
Kyumins-iMac:gcjlib math4tots$ ./a.out 
sum is -4444
absolute value is 4444

So the only issue seems to be that g++ fails to acknowledge /usr/local .所以唯一的问题似乎是g++无法确认/usr/local

But it is tedious to type all this out all the time, especially when I'm just writing small single file programs.但是一直将所有这些都键入是很乏味的,尤其是当我只是在编写小型单文件程序时。

Is there a way for me to get g++ to acknowledge the stuff in /usr/local by default?有没有办法让g++确认/usr/local中的内容? Is there a standard way homebrew users do this?是否有homebrew用户执行此操作的标准方式?


I'm on OS X 10.9.3 with Xcode 5.1.1 in case it is relevant.如果相关,我在 OS X 10.9.3 上使用 Xcode 5.1.1。

I also use Homebrew and had a similar problem on Mac OSX Maverick 10.9.5 and Xcode 6.0.1, but it was solved by running:我也使用 Homebrew 并在 Mac OSX Maverick 10.9.5 和 Xcode 6.0.1 上遇到了类似的问题,但通过运行解决了:

xcode-select --install

Note that it doesn't work without the double hyphens given by the previous answer.请注意,如果没有上一个答案给出的双连字符,它就不起作用。 This installs the command-line tools that also create /usr/lib/ and /usr/include/ .这将安装还创建/usr/lib//usr/include/的命令行工具。 I don't know why Homebrew doesn't automatically check this upon installation, since it does check for Xcode...我不知道为什么 Homebrew 在安装时不会自动检查这个,因为它会检查 Xcode ......

If you want to check exactly what folders your compiler is looking through for header files you can write:如果您想确切地检查您的编译器正在查找头文件的文件夹,您可以编写:

cpp -v

A workaround would be to:一种解决方法是:

export C_INCLUDE_PATH=/usr/local/include
export CPLUS_INCLUDE_PATH=/usr/local/include

At least this tricked the pre-processor to behave here :)至少这欺骗了预处理器在这里表现:)

Try running xcode-select --install尝试运行xcode-select --install

At least on Mavericks, I've found that if I install the Xcode application without installing the command-line tools, then the tools are sort of available, but normal unix-ey builds don't work correctly.至少在 Mavericks 上,我发现如果我在没有安装命令行工具的情况下安装 Xcode 应用程序,那么这些工具是可用的,但是正常的 unix-ey 构建不能正常工作。 One symptom is that /usr/local/include is not on the include search path.一种症状是/usr/local/include不在包含搜索路径上。 The command-line tools seem to resolve this issue.命令行工具似乎解决了这个问题。

I have Yosemite 10.10.5 and running xcode-select --install didn't fix the problem for me.我有 Yosemite 10.10.5 并且运行xcode-select --install没有解决我的问题。 The command returned with xcode-select: error: command line tools are already installed, use "Software Update" to install updates . xcode-select: error: command line tools are already installed, use "Software Update" to install updates返回的命令xcode-select: error: command line tools are already installed, use "Software Update" to install updates

When I ran xcode-select -p , it showed /Applications/Xcode.app/Contents/Developer .当我运行xcode-select -p ,它显示/Applications/Xcode.app/Contents/Developer I ended up deleting Xcode from the Applications directory, which resulted in xcode-select -p returning /Library/Developer/CommandLineTools .我最终从 Applications 目录中删除了 Xcode,这导致xcode-select -p返回/Library/Developer/CommandLineTools This fixed compiler error for me.这对我来说是固定的编译器错误。

That was helpful for me:这对我很有帮助:

Use the latest version.使用最新版本。 1.0.2o_1 just a current build. 1.0.2o_1只是当前版本。

brew install openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/include/openssl /usr/local/include/openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/lib /usr/local/lib/openssl

CMake always adds -isysroot explicitly to get the macosx SDK. CMake 总是显式添加 -isysroot 以获取 macosx SDK。 This behavior that was once required to get anything to compile on the command line.这种行为曾经需要在命令行上编译任何东西。 One can see that the flag affects whether /usr/local/include is considered an implicit include directory by the compiler.可以看出,该标志会影响 /usr/local/include 是否被编译器视为隐式包含目录。 The above command-line examples show that macOS command-line compilers no longer require an explicit -isysroot.上述命令行示例显示 macOS 命令行编译器不再需要显式 -isysroot。 However, since commit 1f085e11 CMake resolves /usr/bin/cc, /usr/bin/c++, and other macOS compiler wrappers that find a real compiler through xcrun to the path to that real compiler.但是,由于提交 1f085e11 CMake 将 /usr/bin/cc、/usr/bin/c++ 和其他通过 xcrun 找到真实编译器的 macOS 编译器包装器解析到该真实编译器的路径。 In our case, AudioConverterLib is an external dependency.在我们的例子中,AudioConverterLib 是一个外部依赖项。 The convention in CMake projects is to not rely on system include directories for anything outside the standard library. CMake 项目中的约定是不依赖系统包含目录来获取标准库之外的任何内容。 Therefore it should use find_package(AudioConverterLib) with a FindAudioConverterLibConfic.cmake find module.因此它应该使用 find_package(AudioConverterLib) 和 FindAudioConverterLibConfic.cmake 查找模块。 Short of a full find module, I ask CMake to find the external header file and add the appropriate include directory using this patch:缺少完整的查找模块,我要求 CMake 查找外部 header 文件并使用此补丁添加适当的包含目录:

find_path(MYLIB_INCLUDE_DIR NAMES MyLib_global.h)
 
target_include_directories(MyClientApp PRIVATE
  "${PROJECT_SOURCE_DIR}/include" ${MYLIB_INC

You can find the whole post here: https://michae9.wordpress.com/2022/09/01/shared-lib-to-be-used-by-client-programs-with-cmake/你可以在这里找到整个帖子: https://michae9.wordpress.com/2022/09/01/shared-lib-to-be-used-by-client-programs-with-cmake/

There are a few questions around this topic with answers that suggest putting a symlink in /usr/local/include .围绕这个主题有几个问题的答案建议在/usr/local/include中放置一个符号链接。 However I'm running macOS Monterey 12.3 (on an M1 MacBook) and that directory doesn't exist.但是我正在运行 macOS Monterey 12.3(在 M1 MacBook 上)并且该目录不存在。

I had installed the Xcode command line tools by downloading the package from Apple, so xcode-select --install just tells me it's already installed and doesn't create any directories.我通过从 Apple 下载 package 安装了 Xcode 命令行工具,所以xcode-select --install只是告诉我它已经安装并且没有创建任何目录。

I ran cpp -v to see which directories are searched for #include <...> :我运行cpp -v以查看在哪些目录中搜索#include <...>

 /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)

I picked /Library/Developer/CommandLineTools/usr/include for the symlink.我选择/Library/Developer/CommandLineTools/usr/include作为符号链接。 In that directory, I ran the following command (note the new location for Homebrew installations, under /opt/homebrew - some old answers are also out of date on this point):在该目录中,我运行了以下命令(注意 Homebrew 安装的新位置,在/opt/homebrew下——一些旧的答案在这一点上也已过时):

sudo ln -s /opt/homebrew/opt/openssl@3/include/openssl .

clang was then able to find the OpenSSL files.然后clang能够找到 OpenSSL 文件。

If you want a solution just for compiling and running like I needed, you can use 如果您想要一个仅用于编译和运行的解决方案,您可以使用

gcc file.c \
-I /usr/local/opt/openssl/include \
-L /usr/local/opt/openssl/lib -lcrypto

-I is to perform an incremental link - 我是要执行增量链接

-L is to search the library given when linking -L是搜索链接时给定的库

apk add --no-cache build-base it works fine !!!! go build -tags musl -o main main.go apk add --no-cache build-base 它工作正常!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使/usr/local/lib 成为mac os x 上ld 的默认库搜索路径? - Make /usr/local/lib a default library search path for ld on mac os x? 在mac osx中获取usr / local路径 - Get usr/local path in mac osx XCode无法找到zmq.h,即使它位于/ usr / local / include和标头搜索路径中 - XCode unable to find zmq.h even though it's in /usr/local/include and the header search path Xcode 7.1无法从/ usr / local / lib库链接 - Xcode 7.1 not linking from /usr/local/lib library 捆绑的动态库:继续查看usr / local / lib - Bundled dynamic library : keep looking in the usr/local/lib dyld:未加载库:/usr/local/lib/libmpfr.4.dylib - dyld: Library not loaded: /usr/local/lib/libmpfr.4.dylib Mac中缺少/ usr / include文件夹 - /usr/include folder missing in mac ld:找不到-l的库:/usr/local/lib/libopencv_xphoto.3.0.0.dylib - ld: library not found for -l:/usr/local/lib/libopencv_xphoto.3.0.0.dylib “库未加载:/usr/local/opt/openssl/lib/libssl.1.0.0.dylib”与 Xcode 8? - "Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib" with Xcode 8? 命令 PhaseScriptExecution 失败,退出代码非零,Xcode 10.1 dyld:库未加载:/usr/local/opt/readline/lib/libreadline.7.dylib - Command PhaseScriptExecution failed with a nonzero exit code,Xcode 10.1 dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM