How to set C++ environment variable in Linux ubuntu? - c++

I just installed Ubuntu on my Virtualbox on Windows.
I was trying to install cmake and the installation guide in the cmake website asked me to do the following steps
./bootstrap
make
make install
But when I just did the ./bootstrap command I get the following list of errors, can anyone suggest me how I can set the C++ compiler on my system. As I just installed Ubuntu perhaps the C++ is not set currently.
Kindly help.
CMake 2.8.1, Copyright 2000-2009 Kitware, Inc.
C compiler on this system is: cc
Error when bootstrapping CMake:
Cannot find appropriate C++ compiler on this system.
Please specify one using environment variable CXX.
See cmake_bootstrap.log for compilers attempted.
Log of errors: /home/vikboy/Downloads/cmake-2.8.1/Bootstrap.cmk/cmake_bootstrap.log

Install the build-essential packages via apt.
sudo apt-get install build-essential

I am having the same problem. Even I defined the CXX environmental variable correctly.
$CXX --version
g++ (GCC) 5.3.0
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.
I looked at the log file of the ./configure output:
Bootstrap.cmk/cmake_bootstrap.log
class NeedCXX
{
public:
NeedCXX() { this->Foo = 1; }
int GetFoo() { return this->Foo; }
private:
int Foo;
};
int main()
{
NeedCXX c;
#ifdef TEST3
cout << c.GetFoo() << endl;
#else
std::cout << c.GetFoo() << std::endl;
#endif
return 0;
}
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
Test failed to compile
Try: /usr/local/bin/g++
Line: /usr/local/bin/g++ -DTEST2 cmake_bootstrap_18998_test.cxx -o cmake_bootstrap_18998_test
---------- file -----------------------
The main problem is that the programmer who wrote the test did not include the
include path -L/PATH/to/C++/header, that on my computer is
/usr/local/include/c++/5.3.0
This path should be included in the auto probing or should be an option in the configure program. I hope the cmake developers can see this message and change the auto detect a little bit.
Or I may be wrong.

Related

How to configure g++ as the default compiler in Mac OS (M1)

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.

Can't cross-compile gdb with MinGW

I'm trying to cross-compile gdb so I could debug my Windows version of program.
My host system is Arch Linux(5.13.4-arch1-1 to be exact).
I'm using MinGW from the official repositories.
$ i686-w64-mingw32-gcc --version
i686-w64-mingw32-gcc (GCC) 11.2.0
Copyright (C) 2021 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 downloaded the source for gdb from https://ftp.gnu.org/gnu/gdb/ (gdb-10.2.tar.gz).
I unpacked it, cd into dir and ran:
$ ./configure --host=i686-w64-mingw32
$ make
For long it compiled without any errors and then:
CXX source-cache.o
source-cache.c:37:10: fatal error: srchilite/sourcehighlight.h: No such file or directory
37 | #include <srchilite/sourcehighlight.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
And also although compiler already compiled gdbserver and other different programs, I can't run them.
Launching gdbserver.exe with wine gives:
$ wine gdbserver.exe
0024:err:module:import_dll Library libgcc_s_dw2-1.dll (which is needed by L"Z:\\home\\udalny\\.local\\build\\gdb\\gdb-10.2\\gdbserver\\gdbserver.exe") not found
0024:err:module:import_dll Library libstdc++-6.dll (which is needed by L"Z:\\home\\udalny\\.local\\build\\gdb\\gdb-10.2\\gdbserver\\gdbserver.exe") not found
0024:err:module:LdrInitializeThunk Importing dlls for L"Z:\\home\\udalny\\.local\\build\\gdb\\gdb-10.2\\gdbserver\\gdbserver.exe" failed, status c0000135
Is it not statically linked?
Does anybody know how to fix it?
You can avoid the problem with the configure flag --disable-source-highlight if you can do without sourxe highlighting.

Cannot cross-compile for arm with c++17

I am trying to cross-compile c++14/c++17 code for my raspberry pi. I am sure it worked on my old PC. However, when I try to compile it on my new pc, it returns me the following error.
$ arm-linux-gnueabihf-g++ main.cpp -std=c++17
arm-linux-gnueabihf-g++: error: unrecognized command line option ‘-std=c++17’
I thought it is the compiler issue, the version of the compiler I am using is:
$ arm-linux-gnueabihf-g++ --version
arm-linux-gnueabihf-g++ (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03) 4.8.3 20140303 (prerelease)
Copyright (C) 2013 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 try to get the newest compiler with
sudo apt-get install libc6-armel-cross libc6-dev-armel-cross binutils-arm-linux-gnueabi libncurses5-dev build-essential bison flex libssl-dev bc
Yet, the version is still the same, which can only compile c++11.
How can I update the arm-linux-gnueabi-g++ compiler?? I am pretty sure there is a compiler that works since I used it before, but not sure how I get it.
I found the easiest way to solve this issue is to directly install the newest cross-compiler
sudo apt-get install g++-9-arm-linux-gnueabihf
and use the compiler as (just an example of using c++17)
arm-linux-gnueabihf-g++-9 main.cpp -std=c++17

g++.exe: installation problem, cannot exec `as': No such file or directory

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.

Undefined reference using Allegro 5 with mingw-w64

I'm trying to compile an Allegro 5 program on Windows 10 with mingw-w64.
I already had installed mingw-w64. Output from g++ --version is:
g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
Copyright (C) 2017 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 downloaded windows binaries for Allegro 5 from https://github.com/liballeg/allegro5/releases (File: allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip) and unzipped the file into C:/allegro5 so now I have C:/allegro5/bin, C:/allegro5/include, C:/allegro5/lib.
A small test program:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
al_init();
return 0;
}
And finally the command I run to compile: g++ test.cpp -I"C:/allegro5/include" -L"C:/allegro5/lib" -lallegro (There is a lib file called liballegro.dll.a under C:/allegro5/lib)
But there are some problems while linking:
C:\Users\xxxx\AppData\Local\Temp\ccg5z97Y.o:test.cpp:(.text+0x1e): undefined reference to `al_install_system'
collect2.exe: error: ld returned 1 exit status
A) What may be the reason for this ?
B) What should I do to compile in a static way ? Is changing -lallegro to -lallegro-static enough ?
This:
g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
is one of the the 32-bit GCC variants provided by MinGW-W64. You are attempting to link the
32-bit code it generates with the 64-bit libraries provided in:
allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip
which will not work. Replace your compiler with the appropriate 64-bit variant x86_64-posix-seh