Failing to compile GTest with Cygwin - c++

I'm new to C++, I am trying to compile gtest with Cygwin. I have installed the GNU g++ compiler which works fine. I ran the following command on Cygwin:
g++ -I /cygdrive/c/devel/cpp/gtest/include -I /cygdrive/c/devel/cpp/gtest -pthread -c /cygdrive/c/devel/cpp/gtest/googletest/src/gtest-all.cc
/cygdrive/c/devel/cpp/gtest/googletest/src/gtest-all.cc:39:25: fatal error: gtest/gtest.h: No such file or directory
compilation terminated.
All the files seem to be in place, however the error does not go, any ideas why?

It looks like you have provided space between -I and path.
There should not be any space between -I and corresponding path.
It should be like
g++ -I/cygdrive/c/devel/cpp/gtest/include -I/cygdrive/c/devel/cpp/gtes..
Check it.

Related

How do I compile my C++ code with anaconda boost?

I have to use Anaconda provided compilers and their Boost package to compile my C++ code. I have a problem such that I do not know how to link properly during the compilation command.
To begin with: I create a dedicated anaconda env. with the following packages:
- boost-cpp=1.77.0
- compilers=1.3.0
These packages may be found here:
https://anaconda.org/conda-forge/compilers
https://anaconda.org/conda-forge/boost-cpp
Upon env. activation: double check which compiler I use:
$ which g++
/usr/share/miniconda3/envs/MYNAME/bin/g++
And then I would like to compile, but I don't know how to include boost libraries.
When I run:
$ g++ workflow/src/CODE.cpp -o workflow/bin/CODE -lm
Of course I get:
CODE.h:10:10: fatal error: boost/property_tree/info_parser.hpp: No such file or directory
10 | #include <boost/property_tree/info_parser.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Nevermind, I have worked it out.
Although it might be useful for others so let's keep the solution in here:
g++ workflow/src/CODE.cpp -o workflow/bin/CODE -I$HOME/miniconda3/envs/MYNAME/include -lm

Compile C++ on Windows

I'm trying to compile C++ on Windows.
The command needed to compile on Linux is:
g++ -O3 -Wall -shared -std=c++11 -fPIC `python -m pybind11 --includes` EO_functions_bipartite.cpp -o extremal_bi.so
I installed MinGW but when I try to compile I get the following error:
g++.exe: error: python: No such file or directory
g++.exe: error: pybind11: No such file or directory
g++.exe: error: unrecognized command line option '-m'
g++.exe: error: unrecognized command line option '--includes EO_functions_bipartite.cpp'
g++.exe: fatal error: no input files
compilation terminated.
Assuming you have python in your path.
The backtick escape thing that embeds the python -m pybind11 --includes command within the g++ doesn't work on cmd.exe in Windows.
Run the python -m pybind11 --includes command on its own line in the cmd shell. Take the output of that command and substitute in into the g++ command. It should be a bunch of -I include params.
So if the output of the python command is this:
-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\Include -IC:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pybind11\include
Expand your g++ command to be this:
g++ -O3 -Wall -shared -std=c++11 -fPIC "-IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\Include" -IC:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pybind11\include EO_functions_bipartite.cpp -o extremal_bi.so
Notice the quotes I put around the first include directory because it has a space in its path.
The easiest way to start on native Windows if you have a Linux background is to install MSYS2 shell with MinGW-w64. This will provide an actual bash that allows you to run commands almost exactly the same way as on Linux, including support for backticks like in the case of your issue.
Though I would always recommend using $( ... ) instead of backticks, as this allows nesting.
Note that MinGW-w64 also exists on Windows to allow cross-building for Windows from Linux, but that may be a bit more difficult if you have never done any cross-building before.
Also -shared ... -o extremal_bi.so in your command should be replaced with -shared ... -o extremal_bi.dll -Wl,--out-implib,libextremal_bi.dll.a as .so files don't exist on Windows as Windows uses .dll files for shared libraries and the compiler uses .dll.a files as library objects for them.
Finally on Windows you need to tell the compiler or linker which symbols you will be exporting by either writing a libextremal_bi.def starting with the line EXPORTS followed all the symbols you want to be exported and include -def libextremal_bi.def in the link command, or using __declspec(dllexport)/__declspec(dllimport) when defining those symbols, which may be a bit complexer as it requires some conditional defines to determine if the code is being compiled for Windows and if it's during the actual build process of the shared library (__declspec(dllexport)) or code that uses it (__declspec(dllimport)). There is also another method to export all symbols, but that's a dirty method that may more easily cause symbol conflicts.

mingw64 (win8.1) how to let him see boost libs?

I've installed at my win8.1 mingw and I would like to compile my program.
When I use command:
g++ -o test test.cpp -lboost_unit_test_framework-mt
I got an error:
no such file od directory:
"#include boost/test/unit_test.hpp" // in "<>" ofc.
when I use my msVS it works fine.
You also must use the -L and the -I option to specify the directories where the boost libraries and headers are located:
g++ -I<path_to_headers> -o test test.cpp -L<path_to_library> -lboost_unit_test_framework-mt
Note for the headers path that should be the path containing the boost directory.

why self built g++ compiler fails to compile my code

I wanted to use latest g++ compiler(4.9.1) on suse linux, but suse only supports an older g++ version. So, I took a latest source code from one of the gnu mirror sites and compiled it myself. Everything went fine. But when I tried to compile my test code using the built g++, the compilation fails with error,
"/root/home/include/c++/4.9.1/x86_64-unknown-linux-gnu/bits/os_defines.h:39:22: fatal error: features.h: No such file or directory".
I can find a "features.h" in "/root/home/include/c++/4.9.1/parallel", but I feel that it should be there in "/root/home/include/c++/4.9.1/" itself.
I copied "/root/home/include/c++/4.9.1/parallel/features.h" to "/root/home/include/c++/4.9.1/" just to see what happens. Now it complains with error "whcar.h" not found.
Have I missed something.
Here are the steps I followed to build g++.
1. /root/home/gcc_build/objdir# ../gcc-4.9.1/configure --prefix=/root/home/ --disable-multilib
2. /root/home/gcc_build/objdir# make -j16
3. /root/home/gcc_build/objdir# make install
4. /root/home/gcc_build/test# /root/home/bin/g++ --sysroot /root/home -m64 test.cpp
I resolved the issue by removing sysroot option and pointing c++ include and library path to my home directory. One thing I noticed was that the g++ source does not come with libc and c header files, and libc has to be installed seperately. But with sysroot option, g++ was trying to look all the header files in my home directory.
Below is the command I used to successfully compile the code.
/root/home/bin/g++ -I /root/home/include/c++/4.9.1 -L /root/home/lib64 -Wl,--rpath=/root/home/lib64 --std=c++0x -m64 test.cpp
Take a look at the GCC Directory Options. It is important to use the correct "specifier" as well (-isystem, -L, -B, -I etc)

G++ compilation, difference between mac os and ubuntu

I have a C++ project looks like the following,
-project/
-include/ --> .hpp files
-src/ --> .cpp files
-bin/ --> for output executable
-makefile
My makefile is the following,
g++ src/* -I include/ -o bin/program
When I run the makefile, it works perfectly on my Mac OS. However I tried to compile it using my Ubuntu and it did not work. I have the following error,
src/SomeFileName.cpp:1:28: fatal error: SomeFileName.hpp: No such file or directory
compilation terminated.
src/main.cpp:1:28: fatal error: SomeFileName.hpp: No such file or directory
compilation terminated.
Should I change some part of the makefile for Ubuntu specifically? What is the problem?
My ubuntu g++ version:
x#x:~$ g++ --version
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
My main.cpp is the following,
#include "ListController.hpp"
#include <time.h>
int main() {
doStuff();
return 0;
}
The only one thing that I can suppose, that on your Mac the PATH environment variable also contains the ./
So, you can try to change your makefile the following way:
g++ ./src/* -I ./include/ -o ./bin/program