I'm setting up a shared conda build environment to help me collaborate with a colleague.
I'm fairly new to conda and have only used it for python programming before. But now I'd like to use it for c++ projects too.
I've run conda install -c gcc which has installed gcc and g++. I can verify this with the output of g++ --version.
>g++ --version
g++ (GCC) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
When I do this outside my conda environment I get this:
>g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
So I know that the install has worked.
I have this hello world code:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
And try compiling it like this:
g++ main.cpp -o main
But I get this error message:
g++ main.cpp -o main
In file included from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/bits/postypes.h:40:0,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/iosfwd:40,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/ios:38,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/ostream:38,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/iostream:39,
from main.cpp:1:
/Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/cwchar:44:19: fatal error: wchar.h: No such file or directory
#include <wchar.h>
^
compilation terminated.
How can I best resolve this?
Related
So, I wanted to use some header files native to GNU C++:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
I read that in MacOS, gcc and g++ are both linked to clang. So, we had to install the gcc using homebrew and use that.
But after installing gcc using homebrew. When I run
g++ --version
I get
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
But running g++-12 --version I get:
g++-12 (Homebrew GCC 12.2.0) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
My VSCode runs g++ (Apple One) to compile C/C++ files. For the goal I wanted to accomplish at the start, I read that we need to have the g++ (installed using homebrew) do the compiling.
So, I ran the following commands:
cd /opt/homebrew/bin
ls -s g++-12 g++
But now, even when I compile the following code:
#include <iostream>
int main()
{
std::cout << 1;
}
I get the following error:
In file included from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/bits/postypes.h:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iosfwd:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ios:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ostream:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iostream:39,
from test.cpp:1:
/opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/cwchar:44:10: fatal error: wchar.h: No such file or directory
44 | #include <wchar.h>
| ^~~~~~~~~
compilation terminated.
Now, removing the link using rm g++ reverts back to my original configuration. But that configuration can't run the headers I requested at the start. Is there any way to solve this?
Most of the above have mostly been taken from here. But I didn't find any solution. But, I have seen people using the same and getting successful.
EDIT:
I found a website where there was a solution. It was to compile not using g++. Rather use
g++-12 --sysroot=$(xcrun --show-sdk-path)
When I used this, it solved the problem. Can someone explain why this happened?
The header error likely indicates the g++ you installed via Homebrew may not be compatible with the Apple Xcode version installed in the macos system directories.
The solution is probably to reinstall one or both packages.
EDIT:
g++-12 --sysroot=$(xcrun --show-sdk-path) changes the search path for system header includes from the default (which was probably set when homebrew installed g++) to the one provided by the Xcode SDK currently installed.
I am trying to run a simple leak check program.
#include <iostream>
int main()
{
double *ptr = new double(3.14);
}
using the command
g++ -g -fsanitize=leak -o main main.cpp
and I get the following error:
clang: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin20.1.0'
I stopped using the clang that comes with Xcode and installed clang/LLVM using homebrew.
$ which clang++
/usr/local/opt/llvm/bin/clang++
clang++ --version
clang version 11.0.0
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
EDIT: When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks to #cigien for pointing it out. g++ still uses default to the compiler that that comes with Apple clang.
g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
According to this answer you should use:
g++-10 -g -fsanitize=leak -o main main.cpp (in this cases (with flag leak) there is no leak message for me ./main, but it compile) then it should be:
g++-10 -fsanitize=address -g main.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out and it detect leak well.
Note, that the correct path of brew installed g++ in MacOS is:
$ which g++-10
> /usr/local/bin/g++-10
--
$ which g++
> /usr/bin/g++ //this is pseudonym of clang
The same for gcc-10 (10 is my current version. You should use your version instead of that)
If you use CMakeLists.txt file you will configure it like this:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")
# leak sanitizer_name not works for me. should be address
And should execute cmake command like this:
cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 ..
And then ASAN_OPTIONS=detect_leaks=1 ./a.out
Note, that if you open */CMakeFiles/3.18.4/CMakeCXXCompiler.cmake file you will observe the compiled info, and now it will be g++.
I'm trying to learn C++, I downloaded and installed g++ on Windows using Cygwin, following this tutorial:
https://www.cs.odu.edu/~zeil/cs250PreTest/latest/Public/installingACompiler/#installing-the-mingw-compiler
Just like in the tutorial, at the end, I check that g++ is installed:
PS D:\Desktop\coursera_ODS_in_c-\Week 2> g++ --version
g++.exe (GCC) 3.4.5 (mingw-vista special r3)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
and then I try to run this code:
#include <iostream>
int main() {
int num = 7;
std::cout << "Values:" << num << std::endl;
std::cout << "Address: " << &num << std::endl;
return 0;
}
by using this
PS D:\Desktop\coursera_ODS_in_c-\Week 2> g++ main.cpp
but I get this:
g++.exe: installation problem, cannot exec `as': No such file or directory
Do you how to solve this ?
The message:
g++.exe: installation problem
clearly states that something's wrong with the installation of your G++ compiler. Also, 3.4.5 version is too old. The current stable release is 10.2 (released on July 23, 2020), reinstalling it with the latest version will probably solve your issue. (Since insufficient details are given, the error cause is still a mystery.)
Try downloading the GNU GCC compiler which comes with the latest edition: GNU GCC.
OTOH, the program is coded properly, no error reported on OnlineGDB.
I am new to mingw/C++ and would like to compile and run the following code I found online:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Unfortunately, running a g++ helloworld.cpp -o helloworld.exe throws the following error:
In file included from c:\mingw\include\_mingw.h:73,
from c:\mingw\include\wchar.h:53,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\cwchar:44,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\postypes.h:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
from helloworld.cpp:1:
c:\mingw\include\w32api.h:59:10: fatal error: sdkddkver.h: No such file or directory
#include <sdkddkver.h>
^~~~~~~~~~~~~
compilation terminated.
I have the impression this is related to Windows SDK and triggered by #include <iostream>. What is <iostream>, why is it important here and how can I get it to compile with g++?
EDIT
g++ version information
D:\TEMP\mingw_example>g++ --version
g++ (MinGW.org GCC-8.2.0-5) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I installed gcc using brew because I needed to install brew's Fortran package. It seems that the gcc package installed by brew install gcc does not support the mdll flag. How can I install the correct versions of Fortran and gcc?
$ which gcc
/usr/local/bin/gcc
$ gcc --version
gcc (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -mdll
gcc: error: unrecognized command line option ‘-mdll’
gcc: fatal error: no input files
compilation terminated.
According to the gcc(1) manpage:
-mdll
This option is available for Cygwin and MinGW targets. It
specifies that a DLL---a dynamic link library---is to be generated,
enabling the selection of the required runtime startup object and
entry point.
Since OS X isn't either Cygwin or MinGW, it's safe to assume that no version of GCC will enable it on OS X.
If you're trying to build a dynamically linked object ("shared object" in UNIX terminology), check out the -dynamic/-dynamiclib flags. They seem to be the rough equivalent on OS X.