Basic CMake C++ WSL project fails to compile - c++

I am trying to use the Build --> Compile functionality of Visual Studio in an almost "Hello World!" program.
On a new CMake Project, I set up CMakeSettings configuration to WSL-Clang-Debug.
Then I edited CMakeProject3.cpp to include some header file (gdal.h), which is found by CMake when it generates its cache.
// CMakeProject3.cpp : Defines the entry point for the application.
//
#include "CMakeProject3.h"
#include "gdal.h"
using namespace std;
int main()
{
cout << "Hello CMake." << endl;
return 0;
}
The CMakeLists.txt file looks like this:
# CMakeList.txt : CMake project for CMakeProject3, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
add_executable (CMakeProject3 "CMakeProject3.cpp" "CMakeProject3.h")
# TODO: Add tests and install targets if needed.
find_package(GDAL)
target_link_libraries(CMakeProject3 PRIVATE GDAL::GDAL)
Building, which I thought meant compiling and linking succeeds, but if I try to compile only, it fails:
cd /mnt/c/Users/user/source/repos/CMakeProject3/out/build/WSL-Clang-Debug;export CFLAGS=-fno-limit-debug-info;export CXXFLAGS=-fno-limit-debug-info;clang++ -fno-limit-debug-info -g -I"C:\Users\user\AppData\Local\Microsoft\Linux\HeaderCache\1.0\wsl_wsl\usr\include\gdal" -c "/mnt/c/Users/user/source/repos/CMakeProject3/CMakeProject3/CMakeProject3.cpp"
Build failed.
If I try to run the exact same command in WSL, it's even more obvious why it fails. It can't find gdal.h. This is not because it doesn't exist under C:\Users\user\AppData\Local\Microsoft\Linux\HeaderCache\1.0\wsl_wsl\usr\include\gdal, but because in wsl it's supposed to be under /mnt/c/Users/andrei/AppData/Local/Microsoft/Linux/HeaderCache/1.0/wsl_wsl/usr/include/gdal
If I run the above command with the correct path style, the compiling succeeds.
How do I convince Visual Studio to not try to use windows style paths in wsl?
If you want to try the exact same example, you'll have to first run these in wsl:
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt update
sudo apt install libgdal-dev
sudo apt install openssh-server g++ gdb make ninja-build rsync zip
Is this a bug in Visual Studio, or am I not setting something somewhere that I should be setting?

Related

Installing opencv From Command Line (Windows)

I am trying to use opencv in a project, and am running into problems 'installing' it. I have extracted the opencv files and have created a small test program:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char **argv){
cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",1);
if (im.empty()){
std::cout << "Cannot open image." << std::endl;
} else {
cv::imshow("image",im);
cv::waitKey(0);
}
return 0;
}
To compile the program I have used the command below:
g++ -I"../../PortableGit/opt/opencv/build/include/" -L"../../PortableGit/opt/opencv/build/x64/vc15/lib" main.cpp -lopencv_core -lopencv_highgui -o main
I get the errors below:
In file included from ../../PortableGit/opt/opencv/build/include/opencv2/core.hpp:3293:0,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui.hpp:46,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui/highgui.hpp:48,
from main.cpp:1:
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:714:14: error: 'recursive_mutex' in namespace 'std' does not name
a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
I believe that it has something to do with mingw binaries no longer being included with opencv. I am missing the opencv/build/x86/mingw directory.
My questions are:
How do I 'install' opencv and use it without also installing some sort of IDE and/or CMake? (I prefer to use vim and the command line.)
Once installed, what command do I use to compile and link a program with opencv?
Any help is appreciated.
Edit:
This appears to be a problem with GCC's implementation of threads on windows. Using mingw-w64 instead of mingw fixed the std::recursive_mutex issue, but now the linker cannot find the proper files.
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_core
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui
After quite a bit of trying things out, this is what I got to work. Oddly, following the LINUX guide to install opencv worked better than the WINDOWS guide, even though I have a windows computer.
Guide to Installing OpenCV on Windows Without VS
Heads-up: This is a multi-step process, 3 separate tools are required. Be prepared for this to take a while.
Part 1: Get everything ready
Download MinGW-w64.
On the downloads page, click on the "MinGW-w64-builds" option. Do not click on the "win-builds" option.
The reason MinGW-w64 has to be used is because it is a newer version of the MinGW compiler suit that has been improved for windows. This means that it supports the posix thread system, where as the standard MinGW compiler only supports the win32 thread system. OpenCV relies on the posix thread system, necessitating the MinGW-w64 compiler.
Extract the MinGW-w64 zip folder to a directory. In my case its PortableGit/opt/MinGW-w64
At this point, you can add the MingGW-w64/mingw32/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the g++ executable directory to run it. This is up to your discretion.
Download an opencv release.
Do not download the package for windows, click the button that says "sources"
Extract the opencv sources zip folder to a directory. In my case its PortableGit/opt/opencv-4.3.0
Also download the opencv_contrib source files directly from the repository.
Extract that folder and place it inside the top level opencv folder: PortableGit/opt/opencv-4.3.0/opencv_contrib in my case.
Download CMake.
I downloaded the zip folder, but you can download the installer if you wish.
Extract the CMake zip folder if you downloaded that, or run the installer. I put my CMake folder here: PortableGit/opt/cmake-3.17.1-win32-x86
At this point, you can add the cmake-3.17.1-win32-x86/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the cmake executable directory to run it. This is up to your discretion.
Part 2: Build OpenCV
Navigate to the opencv directory and create a build folder and cd into it.
mkdir build && cd build
Run the following export commands.
export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
This is to make sure the next cmake command uses the proper compilers.
Run the following cmake command from within that folder:
PortableGit/opt/cmake-3.17.1-win32-x86/cmake.exe -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENCV_VS_VERSIONINFO_SKIP=1 -DOPENCV_EXTRA_MODULES_PATH="/PortableGit/opt/opencv-4.3.0/opencv_contrib/modules/" ..
The -G flag specifies that we are creating build files for the MinGW compiler
The -DCMAKE_BUILD_TYPE=Release specifies that we are building the release version of opencv and not the debug version.
The DOPENCV_EXTRA_MODULES_PATH needs to be set to the modules folder inside the opencv_contrib folder. For me it was PortableGit/opt/opencv-4.3.0/opencv_contrib/modules
The DOPENCV_VS_VERSIONINFO_SKIP specifies to not include version info. If not set, the compiler will throw an error complaining about not having version files. (Shown below for reference.)
gcc: error: long: No such file or directory
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1341:
modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
If successful, the cmake command will finish like this:
Now run this command, again from the build folder: /PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe -j7
mingw32-make.exe is the windows equivalent of the Linux make command.
The -j7 option run the process with a maximum of 7 threads.
This will take a while! It took my laptop ~20 minutes to complete
If the make command ends in an error, make sure to reset your build directory before continuing any troubleshooting. This is done through this series of commands
rm -rf build
mkdir build
cd build
Part 3: Using OpenCV
To use the opencv library that you just compiled in a project of your own, compile the project with these flags from your projects main directory.
Remember that your compiler now has to be set to the mingw-w64 compiler for opencv support.
I added indentation and newlines for readability, but when entering this in the terminal do not include the newlines or indents.
The number at the end of the linker options may change depending on the version of opencv you downloaded. I downloaded opencv-4.3.0, making my number 430, but yours may be different.
PortableGit/opt/MinGW-w64/bin/g++.exe
-I../../PortableGit/opt/opencv-4.3.0/include/
-I../../PortableGit/opt/opencv-4.3.0/build/
-I../../PortableGit/opt/opencv-4.3.0/modules/core/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/calib3d/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/dnn/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/features2d/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/flann/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/gapi/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/highgui/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/imgcodecs/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/imgproc/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/ml/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/objdetect/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/photo/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/stitching/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/ts/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/video/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/videoio/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/world/include/
-L../../PortableGit/opt/opencv-4.3.0/build/lib/
*.hpp
*.cpp
-lopencv_calib3d430
-lopencv_core430
-lopencv_dnn430
-lopencv_features2d430
-lopencv_flann430
-lopencv_highgui430
-lopencv_imgcodecs430
-lopencv_imgproc430
-lopencv_ml430
-lopencv_objdetect430
-lopencv_photo430
-lopencv_stitching430
-lopencv_video430
-lopencv_videoio430
-o
main
Or you could download VS. Up to you. Hope this helps.
Correction for JackCamichael's answer
those 2 commands won't work in Windows
export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
This should be
setx -m CC C:\msys64\mingw64\bin\gcc.exe
setx -m CXX C:\msys64\mingw64\bin\g++.exe
C:\msys64\mingw64\bin is mingw64 path on my machine

Compiling and Running CGAL Triangulation Demo

I am trying to use the CGAL library to display a 2D Delaunay triangulation, like in the example here
My code looks like this:
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/draw_triangulation_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Triangulation;
typedef Triangulation::Point Point;
int main(void){
Point a(1,1), b(2,1), c(2,2), d(1,2);
Triangulation T;
T.insert(a);
T.insert(b);
T.insert(c);
T.insert(d);
CGAL::draw(T);
return 0;
}
When I try to compile this code with g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp the program compiles successfully, but on runtime I get Impossible to draw because CGAL_USE_BASIC_VIEWER is not defined
By searching on Google, I found someone that suggested using g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp -DCGAL_USE_BASIC_VIEWER which produces the following error on compile time: /usr/include/CGAL/Qt/Basic_viewer_qt.h:30:10: fatal error: QApplication: No such file or directory #include <QApplication>
I am using ubuntu 19.04, so I installed CGAl using sudo apt-get install libcgal-dev and sudo apt-get install libcgal-qt5-dev
I tried to install sudo apt-get install libqt5svg5-dev libqt5opengl5-dev as well to solve the error, but to no avail.
Do I need to install additional libraries? Maybe the compilation must be done differently?
Thank you
Ok, for anyone facing the same problem, here is how I solved it:
First, I used the locate QApplication command to find the location of the QApplication header file on my system. Be sure to run sudo updatedb before using locate. If locate doesn't find the location of QApplication then you are missing qt libraries. Try sudo apt-get install qt5-default and the other libraries I mentioned in my question, run sudo updatedb and try locate QApplication again.
When you find the path to QApplication just use the -I option to instruct the compiler to use it. Here is an example g++ -o delaunayTest delaunayTest.cpp -lCGAL -lgmp -lCGAL_Qt5 -DCGAL_USE_BASIC_VIEWER -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/ (because in my case, QApplication was inside the directory /usr/include/x86_64-linux-gnu/qt5/QtWidgets/)
Trying to compile with this, you will probably get another header file error. Repeat the same process using locate until you get no more header file errors.
At that point, you will likely encounter an undefined reference to symbol error.
To solve this, use locate again to find the location of the file that caused the error (for example libQt5OpenGL.so.5) and add the path to the compilation command as is (for example g++ -o delaunayTest delaunayTest.cpp /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so.5) along with all the previous options.
You will probably get several undefined reference to symbol errors as well. Just keep using the same method until you don't get any.
At this point the program should compile properly and run properly.
Note that if you have multiple versions of qt installed, then the above might not work properly (If for example you have software that uses qt like MATLAB or anaconda installed in your system. You will know because locate will produce many paths for each file on the steps above). In such a case, I suggest building a Virtual Machine, downloading the CGAL libraries and qt5-default and following the above steps there, since it is very likely this won't work in a system with multiple qt installations.
Another option (maybe the easiest), using CMake, is to generate the file using the builtin script:
From the source file directory, run cgal_create_CMakeLists -c Qt5
Edit the generated CMakeLists.txt adding the line add_definitions(-DCGAL_USE_BASIC_VIEWER)
My generated and edited CMakeLists.txt:
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
cmake_minimum_required(VERSION 3.1...3.15)
project( tmp_cgal )
# CGAL and its components
find_package( CGAL QUIET COMPONENTS Qt5 )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
add_definitions(-DCGAL_USE_BASIC_VIEWER) # <==== I've added this
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
# include for local package
# Creating entries for all C++ files with "main" routine
# ##########################################################
create_single_source_cgal_program( "b.cpp" )
Create a build directory: mkdir build
Change directory: cd build
Generate the build files: cmake ..
Build: make
Run the binary file. For me it's ./b because my source file was b.cpp
Environment:
These instructions should work for an Ubuntu 20.04.1 (or similar) with the packages libcgal-dev, libcgal-qt5-dev and qtbase5-dev installed (and, of course, cmake, make and g++).
Main references:
doc1 and doc2

cmake : failing to link against google profiler (gperftools)

I am on ubuntu 16.04.
Here what I did:
installed gperftools :
sudo apt-get install google-perftools libgoogle-perftools-dev
downloaded FindGperftools.cmake from: https://github.com/vast-io/vast/blob/master/cmake/FindGperftools.cmake
renamed FindGperftools.cmake to GperftoolsConfig.cmake and placed it in a cmake folder in my package
added to CMakelists.txt:
set (Gperftools_DIR "${CMAKE_CURRENT_LIST_DIR}/cmake/")
find_package(Gperftools REQUIRED)
in same CMakelists.txt, link my executable:
target_link_libraries(my_executable ${GPERFTOOLS_PROFILER})
in a terminal, export the CPUPROFILE environment variable:
export CPUPROFILE=/my_path/prof.out
in the same terminal, run the executable:
./my_executable
There is no error message, but the log file /my_path/prof.out is not created.
If I run "ldd" on "my_executable", it does not show any linkage against profiler (ldd ./my_executable | grep profil does not result in anything).
Yet, when looking at files in the build folder, the compiler seems to do the linkage (-lprofile is there).
Anything I may have forgotten?
Note: not sure it is relevant, but I use catkin.
This looks like ubuntu's (and non-standard) linker feature to not link libraries which symbols are directly not used. Try adding -Wl,-no-as-needed to your LDFLAGS (and make sure it is passed before -lprofiler).

Can't run bjam to compile boost python tutorial

I am trying to follow this tutorial on wrapping C++ code for python for Windows.I installed python.Downloaded the latest version of boost(1_55).First I ran bootstrap.bat to build bjam.exe.Next, I configured boost_1_55_0\tools\build\v2\user-config.jam to use msvc10 compiler and added the path to python installation.
Now,based on the tutorial :
Now we are ready... Be sure to cd to libs/python/example/tutorial
where the tutorial "hello.cpp" and the "Jamroot" is situated.
Finally:
bjam
Trying to run bjam in that directory gives me : "bjam is not recognized as internal or external command" error.What have I missed here? Should user-config.jam reside in another location?Or bjam added to system path?
UPDATE:
Ok.Thanks to #john I had to add bjam to system path.But now,running it,hello_ext.lib is created in \boost_1_55_0\libs\python\example\tutorial\bin\msvc-11.0\debug but not DLL.Based on the tutorial I should get DLL file for the extension.Now, I am not sure how python links with extension.But if I assume it does like C++ then it should have linked with hello_ext.lib.But if run :
python hello.py
which contains imported method from the extension ,the python runtime crashes.Isn't there a comprehensive tutorial on this workflow?Boost doc sucks completely on this.
This worked for me:
1.) unzip boost_1_55_0.zip
2.) Prepare to use the Boost Library Binaries
Go to the boost_1_55_0 root directory and open a command prompt and type following commands:
Bootstrap
3.) Find user-config.jam:
Type following in the command prompt:
ECHO %HOMEDRIVE%%HOMEPATH%
4.) If the user-config.jam is in your homedrive directory please change it there as followed:
ATTENTION:
The .jam language rates a “whitespace” as a separation of arguments!
# -------------------
# MSVC configuration.
# -------------------
# Configure msvc (default version, searched for in standard locations and PATH).
# using msvc ;
# Configure specific msvc version (searched for in standard locations and PATH).
using msvc : 10.0 : C:\\app\\tools\\MSVisualStudio2010\\VC\\bin\\cl.exe ;
….
# ---------------------
# Python configuration.
# ---------------------
# Configure specific Python version.
# using python : 3.1 : /usr/bin/python3 : /usr/include/python3.1 : /usr/lib ;
using python
: 2.5 # Version
: C:\\app\\tools\\Python25\\python.exe # Python Path
: C:\\app\\tools\\Python25\\include # include path
: C:\\app\\tools\\Python25\\libs # lib path(s)
: <define>BOOST_ALL_NO_LIB=1
;
5.) Build the Libraries AFTER configuration!!
Go to the boost_1_55_0 root directory and open a command prompt and type following commands:
.\b2
6.) Copy the user-config.jam to \boost_1_55_0\libs\python\example\tutorial
7.) Go further to \boost_1_55_0\stage\lib\
Rename libboost_python-vc100-mt-gd-1_55.lib to boost_python-vc100-mt-gd-1_55.lib and copy it to
\boost_1_55_0\libs\python\example\tutorial
8.) Now you should have all of these files in the \boost_1_55_0\libs\python\example\tutorial directory
hello.cpp
hello.py
user-config.jam
Jamroot
boost_python-vc100-mt-gd-1_55.lib
bjam.exe
9.) Open a command prompt in \boost_1_55_0\libs\python\example\tutorial
And type following command:
bjam
10.) After successful building..
You should have this file in the directory:
hello_ext.dll
rename this file to:
hello_ext.pyd

Makefile which link to library in C++

Recently, I try to create an makefile as below:
all:
g++ test.cpp -o Test
It creates an runnable Test. However, If I try to link it to an library (as LibCurl to use SSL connetion):
all:
g++ test.cpp -o Test -lcurl
It goes wrong (I already installed libcurl4-openssl-dev package as standard library)!
What does I miss? Can you give any solution? Thanks!!
Edit: The error is below:
fatal error: curl.h: No such file or directory
I guess, you need fix your sources:
#include <curl/curl.h>
The header files are missing in the include search path.
You will have install the development package of libcurl.
For example, install libcurl-devel-7.19.0-11.1.x86_64.rpm for Open Suse 11.1.
To know the list of files that would be installed by the rpm, download the rpm, type the following command in the downloaded dir.
rpm -qlp <rpmname>
As per the example above, the command would be
rpm -qlp libcurl-devel-7.19.0-11.1.x86_64.rpm
This will list the files along with the path in which it would be installed to.
In our example, the result would look like..
/usr/include/curl /usr/include/curl/curl.h /usr/include/curl/curlbuild.h
/usr/include/curl/curlrules.h
/usr/include/curl/curlver.h and so on...
and
Install the development libcurl package for the version of linux you use and include #include <curl/curl.h> in your code.
Refer http://curl.haxx.se/download.html to download the right one