Problem with C++ compiler on macOS (building cmake project) - c++

I have a 16inch MacBook pro with latest updates (macOS Catalina 10.15.2 (19C57)), the latest version of XCode and Command line tools for macOS (the tools being installed in the usual path /Library/Developer/CommandLineTools/).. Not sure if this is relevant to my issue or not, but I also have the latest version of JetBrains’s CLion installed on my machine (running without issues).
Here is the problem: When trying to build a project with cmake, the build FAILS, giving me this error message:
In file included from /Users/basavyr/Pipeline/DevWorkspace/Github/xrootd/bindings/python/src/PyXRootDFile.cc:25:
In file included from /Users/basavyr/Pipeline/DevWorkspace/Github/xrootd/bindings/python/src/PyXRootD.hh:28:
In file included from /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/Python.h:38:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string.h:61:15: fatal error:
'string.h' file not found
#include_next <string.h>
1 error generated.
error: command '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc' failed with exit status 1
make[2]: *** [bindings/python/python_bindings] Error 1
make[1]: *** [bindings/python/CMakeFiles/python_target.dir/all] Error 2
make: *** [all] Error 2
and this is probably related to my C++ compiler not having the correct path to the include files. To see what the issue could be, I’ve made a simple C++ file and I tried to compile it.
main.cpp
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
int main()
{
std::string name = "NAMEX";
std::cout << name;
}
First, I tried with the usual “g++” and it worked just fine:
basavyr#Roberts-MacBook-Pro stringTestCLANG % g++ main.cpp
basavyr#Roberts-MacBook-Pro stringTestCLANG % ./a.out
NAMEX% basavyr#Roberts-MacBook-Pro stringTestCLANG %
Then, I tried with /Applications/Xcode.app/Contents/Developer/usr/bin/g++ main.cpp and it worked just fine.
Checking the version of “g++” on my machine gives me this:
g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
This is interesting, because I was expecting that the default compiler is installed in /usr/bin/ instead. Going into that “InstalledDir” folder I saw that there is no “g++” binary, but only “clang++” and “c++”, so I tried to compile with both and it turns out that both compilers are giving the same error as the one in the project that I trying to build:
basavyr#Roberts-MacBook-Pro stringTestCLANG % /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ main.cpp
In file included from main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:505:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string_view:176:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__string:57:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:642:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstring:61:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string.h:61:15: fatal error:
'string.h' file not found
#include_next <string.h>
^~~~~~~~~~
1 error generated.
basavyr#Roberts-MacBook-Pro stringTestCLANG %
Since I also have Command Line Tools installed, I tried to compile with both “g++” and “clang++” from /Library/Developer/CommandLineTools/usr/bin/ and the first one compiles successfully, while the second one throws the same error with regards to the string header file.
basavyr#Roberts-MacBook-Pro stringTestCLANG % /Library/Developer/CommandLineTools/usr/bin/c++ main.cpp
In file included from main.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:38:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:216:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:15:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:505:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:642:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/cstring:61:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string.h:61:15: fatal error: 'string.h' file not found
#include_next <string.h>
^~~~~~~~~~
1 error generated.
So, I assume that when I build that project with cmake, it is using one of these compilers (from the XCodeDefault.xctoolchain folder) and it has issues with the string header.
Q1: How can I solve this issue? Q2: And also, why does my “g++” installation directory points to this XCodeDefault.xctoolchain instead of “/usr/bin/’ or “/Applications/Xcode.app/Contents/Developer/usr/bin/”. Is there a way to change that in macOS?
Thank you in advance

Related

MacOS M1 -> fatal error: 'GLFW/glfw3.h' file not found

I have a project (here) that works both on Linux and intel MacOS, but in my Mac m1 it doesn't. Whenever I try to compile it, I get the following errors (ops.: I'm using vscode m1 native):
pedrohaccorsi#MacBook-Air-de-Pedro dev % make
g++ -o app src/glad.c src/AudioManager.cpp src/Character.cpp src/SceneManager.cpp src/Source.cpp src/Sprite.cpp src/stb_image.cpp -g -Iinclude -F/Library/Frameworks -lglfw -ldl -framework SDL2 -I/Library/Frameworks/SDL2.framework/Headers -framework SDL2_mixer -I/Library/Frameworks/SDL2_mixer.framework/Headers
In file included from src/AudioManager.cpp:1:
include/AudioManager.h:1:10: fatal error: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
^~~~~~~~~~~~
1 error generated.
In file included from src/Character.cpp:1:
In file included from include/Character.h:1:
In file included from include/Sprite.h:6:
include/Shader.h:13:10: fatal error: 'GLFW/glfw3.h' file not found
#include <GLFW/glfw3.h>
^~~~~~~~~~~~~~
1 error generated.
In file included from src/SceneManager.cpp:1:
In file included from include/SceneManager.h:3:
include/Shader.h:13:10: fatal error: 'GLFW/glfw3.h' file not found
#include <GLFW/glfw3.h>
^~~~~~~~~~~~~~
1 error generated.
src/Source.cpp:10:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
^
In file included from src/Source.cpp:12:
In file included from include/SceneManager.h:3:
include/Shader.h:13:10: fatal error: 'GLFW/glfw3.h' file not found
#include <GLFW/glfw3.h>
^~~~~~~~~~~~~~
1 warning and 1 error generated.
In file included from src/Sprite.cpp:1:
In file included from include/Sprite.h:6:
include/Shader.h:13:10: fatal error: 'GLFW/glfw3.h' file not found
#include <GLFW/glfw3.h>
^~~~~~~~~~~~~~
1 error generated.
make: *** [app] Error 1
Before hand I ran these three commands and they worked successfully, no errors nor warnings at all, so I assumed it was all working fine.
brew install glfw
brew install sdl2
brew install sdl2_mixer
Anyone has any ideas?
My issue was partially solved. There were two problems, GLFWand SDL2.
GLFW
The fix was the same proposed in this thread; in a nutshell it consists in:
$ nano ~/.zshrc
> export CPATH=/opt/homebrew/include
> export LIBRARY_PATH=/opt/homebrew/lib
This will tell the compiler to look for things in opt/homebrew, which is where homebrew for m1 installs the packages, instead of usr/local.
SDL2
I downloaded the two .dmg files from the websites below
https://www.libsdl.org/download-2.0.php
https://www.libsdl.org/projects/SDL_mixer/
Then I went to the downloads and copied the .framework directory from both downloads and pasted into /Library/Frameworks.
This solved those errors from before, that the system could not find the includes, but a new error showed up: symbols not found for architecture arm64. I decided to quit trying and just removed the sound out of my game (it was a college assignment, no biggie in taking features out).
On MacOS Monterey 12.6 I fixed the issue by installing xcode, then installed with homebrew:
glew
glfw3
xquartz
brew install glew glfw3
brew install --cask xquartz
Then I pointed to the X11 folders and sample OpenGL program compiled.
The github source code was taken from: https://github.com/zamirmf/OpenGLOnMac
For example:
clang -o testopengl main.cpp \
-L/opt/X11/lib -L/opt/homebrew/lib \
-I/opt/X11/include -I/opt/homebrew/include \
-lglfw -framework OpenGL

gcc can't find header file

I have this main in a project in c++ but I can't compile it in gcc due to this error:
In file included from main.cpp:11:
../framework/application.h:8:10: fatal error: includes.h: El fitxer o directori no existeix
8 | #include "includes.h"
| ^~~~~~~~~~~~
What it says is that the file "includes.h" doesn't exist. It exist but is in another directory and I don't know how to compile using a header from another directory.
I tried:
gcc -I ../framework/ main.cpp
gcc -I../framework/ main.cpp
gcc -I "../framework/" main.cpp
Testing on my machine with a similar directory structure using the I directive worked for me. Ensure that your path is correct and I would even try using an absolute path as a test.

"fatal error: boost/regex.hpp: No such file or directory" when trying to compile C++ program using a makefile

I am trying to compile a piece of open source software called "SPECIES
Identification of Taxonomic Mentions in Text". I am on MacOS.
I downloaded the source code (which can be found here), moved into the directory and used the command make to compile. This is the error returned:
g++ -fpic -pthread -Wall -O3 -o organisms organisms.cxx -lm -lboost_regex
In file included from batch_tagger.h:5:0,
from organisms.cxx:3:
tagger.h:7:27: fatal error: boost/regex.hpp: No such file or directory
compilation terminated.
make: *** [organisms] Error 1
I installed the C++ boost library using brew install boostand tried the above steps again (it did not work).
I tried dropping the boost directory into the directory containing the source code (it did not work).
Any suggestions/help?
You need to tell the compiler where to find boost headers.
You need to use the include path option to specify where the boost headers can be find, use -I/path/to/boost/include.
Then include the file using #include <boost/regex.hpp> from your code.

Getting fatal error: 'type_traits' file not found #include <type_traits>, while building clang example

I am trying to build the PrintFunctionNames example from clang.
However I get the following error:
[mac-osx:clang/examples/PrintFunctionNames] osx% clang++ -std=c++0x PrintFunctionNames.cpp
In file included from PrintFunctionNames.cpp:15:
In file included from /usr/local/include/clang/Frontend/FrontendPluginRegistry.h:13:
In file included from /usr/local/include/clang/Frontend/FrontendAction.h:22:
In file included from /usr/local/include/clang/Basic/LLVM.h:22:
In file included from /usr/local/include/llvm/Support/Casting.h:19:
/usr/local/include/llvm/Support/type_traits.h:17:10: fatal error: 'type_traits' file not found
#include <type_traits>
System Info:
clang version 4.0.0 (http://llvm.org/git/clang.git 6197d01def79876e2c1670ced871e10b12c36241) (http://llvm.org/git/llvm.git 24f7cd87f70ddcc91d50f77e405420c0c27853fd)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /usr/local/bin
OSX 10.11.6
Edit 1:
on making the changes suggested by Alex, I start getting header issues. On fixing it via includes, same error is thrown again
% clang++ -std=c++0x PrintFunctionNames.cpp -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -I../../../clang/include -I../../../../include -I../../../../../build/include
In file included from PrintFunctionNames.cpp:15:
In file included from ../../../clang/include/clang/Frontend/FrontendPluginRegistry.h:13:
In file included from ../../../clang/include/clang/Frontend/FrontendAction.h:22:
In file included from ../../../clang/include/clang/Basic/LLVM.h:22:
In file included from ../../../../include/llvm/Support/Casting.h:19:
../../../../include/llvm/Support/type_traits.h:17:10: fatal error: 'type_traits' file not found
#include <type_traits>
^
1 error generated.
OS X (macOS) has a bit different approach. You can have several Xcode installed and you compile for different platforms.
You should use -isysroot option to explicitly specify against which SDK to build.
The following command should do the trick
clang++ -std=c++0x PrintFunctionNames.cpp -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk
Otherwise, please check which SDKs are installed and change the value of isysroot to correspond to your setup.

unistd.h file not found, clang++ and OS X

I'm currently struggling with clang++, and after looking on the web for a long time I decided asking my question. I'm on OS X and using clang++ via Emacs (it's used by flycheck to highlight errors, among other things). I have a very simple file that starts with :
#include <iostream>
And the line is underlined in red. When I go check the clang logs, here's what I have :
In file included from ./point.hpp:4:
In file included from /usr/local/Cellar/llvm/3.6.2/bin/../include/c++/v1/iostream:37:
/usr/local/Cellar/llvm/3.6.2/bin/../include/c++/v1/__config:23:10: fatal error: 'unistd.h' file not found
#include <unistd.h>
So, the error comes from the iostream include, and in iostream there's a #include <unistd.h> that clang++ doesn't like.
I'll say it immediately, I have Xcode AND the command-line tools installed. So my unistd.h should be there. My code also compiles (via g++), so everything is fine there. It's just clang++ that's lost.
Something I notice is that it's going in my llvm folder (that I installed with homebrew) to fetch iostream, that doesn't seem right. He should be getting the iostream in my system, right ? So /usr/local/include/c++/iostream. Could this be the problem ? If so, how can I tell it to get the right iostream ?
And if it's not the problem, how could I solve my unistd.h problem ?
Thanks in advance !
EDIT : With the clang++ -stdlib=libc++ image.cpp command, I get this :
clang++ -stdlib=libc++ image.cpp
In file included from image.cpp:1:
In file included from ./image.hpp:4:
In file included from ./figure.hpp:4:
In file included from ./point.hpp:4:
In file included from /usr/local/Cellar/llvm/3.6.2/bin/../include/c++/v1/iostream:37:
/usr/local/Cellar/llvm/3.6.2/bin/../include/c++/v1/__config:23:10: fatal error: 'unistd.h' file not found
#include <unistd.h>
^
1 error generated.`
So, same problem.
EDIT 2 : I noticed which clang++ would return /usr/local/bin/clang++ , so I tried specifically using the Xcode clang++ and here's the result :
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -stdlib=libc++ image.cpp
In file included from image.cpp:1:
In file included from ./image.hpp:4:
In file included from ./figure.hpp:4:
In file included from ./point.hpp:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:37:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:23:10: fatal error: 'unistd.h' file
not found
#include <unistd.h>
^
1 error generated.
So same problem on the C++ headers bundled with XCode.