Basic CMake C++ WSL project fails to compile

Viewed 806

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?

0 Answers
Related