Resolving Header File Issues in WSL Ubuntu for a VS2022 CMake Project - cmake

Header File Include Issue in WSL Ubuntu with VS2022 and CMake
Problem
I am using Visual Studio 2022 and when I try to include "GLFW/glfw3.h" on WSL Ubuntu, I get an error that the source file doesn't exist. However, the same code works perfectly on Windows. (it's a problem with all other libs too but I just took glfw as an example)
Current Code
cmake_minimum_required (VERSION 3.8)
project ("test project")
include(FetchContent)
FetchContent_Declare(
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.3.8
)
FetchContent_GetProperties(glfw)
if(NOT glfw_POPULATED)
FetchContent_Populate(glfw)
set(GLFW_BUILD_EXAMPLES OFF)
set(GLFW_BUILD_TESTS OFF)
set(GLFW_BUILD_DOCS OFF)
set(GLFW_INSTALL OFF)
add_subdirectory(${glfw_SOURCE_DIR} ${glfw_BINARY_DIR})
endif()
add_executable(test_exe "main.cpp")
target_link_libraries(test_exe PRIVATE glfw)
I found a workaround where I would change FETCHCONTENT_BASE_DIR to the project's source directory but it introduced more problems. For example, switching between Windows and WSL Ubuntu requires deleting the fetched files from Windows, or it results in the error :
The current CMakeCache.txt directory /home/olli/.vs/Omerta/external/glfw/glfw-subbuild/CMakeCache.txt is different than the directory c:/Users/Olli/source/repos/GitOlli/Omerta/external/glfw/glfw-subbuild where CMakeCache.txt was created.
I have also tried explicitly specifying the include directory but it also doesn't work.
target_include_directories(test_exe PRIVATE "${CMAKE_BINARY_DIR}/_deps/glfw-src/include")

The solution was to remove the "out" and ".vs" folders, replace CMakePresets.json with CMakeSettings.json, and then restart the computer.

Related

Setting Cmake Library paths without Editing CmakeLists

Goal: To set Cmake Paths without having to edit the CMakeLists.txt file.
In Linux, using find_library() in Cmake is enough to find a library installed with apt.
In Windows, Cmake is unable to find it automatically.
Note: Please treat sqlite3 as any library.
What is the proper method to set the library paths which cmake searches for my library files without having to edit CMakeLists?
Note: I have samlib compiled to a static library file and placed in some directory away from the project itself.
I have tried the following on Linux and Windows.
cmake_minimum_required(VERSION 3.0.0)
project(SampleProj VERSION 0.1.0)
find_library(samlib NAMES samplelib)
add_executable(SampleProj src/main.cpp)
target_link_libraries(SampleProj ${samlib})```
Try to use CMAKE_LIBRARY_PATH
Semicolon-separated list of directories specifying a search path for the find_library() command. By default it is empty, it is intended to be set by the project. See also CMAKE_SYSTEM_LIBRARY_PATH and CMAKE_PREFIX_PATH.
src: https://cmake.org/cmake/help/latest/variable/CMAKE_LIBRARY_PATH.html
set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
cmake -H. -Bbuild -G "%CMAKE_GENERATOR%" "-DCMAKE_PREFIX_PATH=path_to_samlib"

add_executable create two binaries files instead of one

protoc.cmake:
set(protoc_files
${protobuf_source_dir}/src/google/protobuf/compiler/main.cc
)
add_executable(protoc ${protoc_files})
target_link_libraries(protoc libprotobuf libprotoc)
add_executable(protobuf::protoc ALIAS protoc)
set(PROTOC_NAME "protoc")
set_target_properties(protoc PROPERTIES
VERSION ${protobuf_VERSION}
OUTPUT_NAME ${PROTOC_NAME})
if(WIN32)
set(PROTOC_SUFFIX ".exe")
set_target_properties(protoc PROPERTIES
SUFFIX ${PROTOC_SUFFIX})
else()
set(PROTOC_SUFFIX)
endif()
set(PROTOBUF_PROTOC_EXECUTABLE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROTOC_NAME}${PROTOC_SUFFIX}" PARENT_SCOPE)
I have included source of google protocol buffer compiler to my project, and I use previous cmake file for generating runtime binary, but instead of one file I get two executable files: "protoc" and "protoc-3.6.1".
I am using CLion IDE 2018.1.5 with bundled CMake 3.10.3 on Ubuntu 18.04.
On Windows I don't have a such problem.
As #Tsyvarev said in comment, one of these files is actually a symlink to another one. This is what VERSION property implies.

Issues Configuring CLion, Cmake, and SFML

I am currently trying to configure my Cmake file to include the SFML libraries.
My CMakeLists.txt. I'm using OS X Yosemite if that matter at all.
cmake_minimum_required(VERSION 2.8.4)
project(SFMLTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=lib++")
set(SOURCE_FILES main.cpp)
add_executable(SFMLTest ${SOURCE_FILES})
#Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.2 REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${main.cpp} ${SFML_Libraries})
endif()
and the error I am currently getting is
Error:By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has asked
CMake to find a package configuration file provided by "SFML", but CMake did notfind one.
Could not find a package configuration file provided by "SFML" (requested version 2.2)
with any of the following names:
SFMLConfig.cmake sfml-config.cmake
Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or
set "SFML_DIR"to a directory containing one of the above files.
If "SFML" provides a separate development package or SDK, be sure it has been installed.
my FindSFML.cmake is located at
/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules
Your question says the full path to the find module is
/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules/FindSFML.cmake
But you're adding ${CMAKE_SOURCE_DIR}/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules to your CMAKE_MODULE_PATH. ${CMAKE_SOURCE_DIR} is the absolute path to the directory containing the top-level CMakeLists.txt file. If you know the absolute path to the module file you want to include, you should most certainly not prefix it with the source tree path. Just change the line to this:
set(CMAKE_MODULE_PATH "/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules" ${CMAKE_MODULE_PATH})
Also note that since you have REQUIRED specified among the arguments to find_package(), CMake will terminate with an error if the package cannot be found. Having if(SFML_FOUND) is therefore pointless.
This solution is perfect: https://oxymeos.shost.ca/article.php?about=work_with_the_SFML_in_CLion. I recommend it! It worked for me
You create a folder named "cmake_modules" at the root of the project and you place in this folder the "FindSFML.cmake" file (on Windows and Mac OS X: "[Your_SFML Location]/cmake/Modules/FindSFML.cmake", and on Linux: "[Your_SFML_location]/share/SFML/cmake/Modules/FindSFML.cmake".
The CMake configuration file (CMakeLists.txt) is then presented in this form:
cmake_minimum_required(VERSION 3.7)
project([your_project])
# Define the source and the executable
set(EXECUTABLE_NAME "[name_executable]")
add_executable(${EXECUTABLE_NAME} [project_files])
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()

How to use cmake find_package() with a local copy of the package?

I'm trying to make a project that has both ZLIB and LIBPNG (and other libraries). LibPNG's CMakeLists.txt file has this in it: find_package(ZLIB REQUIRED) It's stock code that comes with it and I don't want to change it.
I'm building on Windows (Visual Studio). This is a cross-platform application (Windows, Mac, Linux and Mobile devices) I cannot rely on /usr/lib versions of any libraries. So I'm building them all with my project together.
I can't get LibPNG to build unless I hack this up. In an upper-level CMakeLists.txt file, I put this in there:
ADD_SUBDIRECTORY(contrib/${CUSTOM_ZLIB_LOCATION})
SET(ZLIB_FOUND ON CACHE BOOL "Yes")
SET(ZLIB_INCLUDE_DIR ${CMAKE_BINARY_DIR}/contrib/${CUSTOM_ZLIB_LOCATION} {CMAKE_SOURCE_DIR}/contrib/${CUSTOM_ZLIB_LOCATION})
SET(ZLIB_LIBRARY zlib CACHE STRING "zlib library name")
This satisfies find_package(ZLIB REQUIRED) But I think this is a hack. Is there some straight forward way to build the local copy of zlib without all the 3 extra lines?
I only added this line at the beginning (at least before find_package(ZLIB REQUIRED)) and it worked for me.
set(ZLIB_ROOT <zlib folder here>)
But others may need doing something like:
if (CMAKE_VERSION VERSION_GREATER 3.12 OR CMAKE_VERSION VERSION_EQUAL 3.12)
# Enable find_package uses of <PackageName>_ROOT variables.
cmake_policy(SET CMP0074 NEW)
endif()
set(ZLIB_ROOT <zlib folder here>)
We set the policy to NEW.
The OLD behavior for this policy is to ignore <PackageName>_ROOT variables.
CMake version 3.22.1 warns when the policy is not set (and defaults to OLD behavior).

Find package Eigen3 for CMake

CMake cannot find my Eigen3 package. I set an environment variable called
EIGEN3_INCLUDE_DIR
pointing to the path where FindEigen3.cmake is.
Then in the CMakelists.txt I wrote:
find_package( Eigen3 REQUIRED )
include_directories( EIGEN3_INCLUDE_DIR )
I get next message of error:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:91 (MESSAGE):
Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)
(Required is at least version "2.91.0")
Call Stack (most recent call first):
C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:252 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindEigen3.cmake:76 (find_package_handle_standard_args)
test/test_quaternion/CMakeLists.txt:25 (find_package)
Any idea on what I am missing or doing wrong?
Since Eigen3 is completely header only, all you ever need is the path to the include directory. And this one, you are already defining manually anyway. So there is no real need for a FindEigen3.cmake or FIND_PACKAGE call.
Simply use
INCLUDE_DIRECTORIES ( "$ENV{EIGEN3_INCLUDE_DIR}" )
or
SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
IF( NOT EIGEN3_INCLUDE_DIR )
MESSAGE( FATAL_ERROR "Please point the environment variable EIGEN3_INCLUDE_DIR to the include directory of your Eigen3 installation.")
ENDIF()
INCLUDE_DIRECTORIES ( "${EIGEN3_INCLUDE_DIR}" )
A few notes:
If you want to access the content of a CMake variable, make sure to use ${...}
$ENV{....} accesses environment variables.
The second example will stop with an error if the environment variable is not set (and, thus, EIGEN3_INCLUDE_DIR cmake variable is empty)
Be careful to use quotation marks around (evaluated) variables if they could contain whitespace. Otherwise, CMake will interpret it as a list.
If you want to use custom find modules, make sure to either place them in you CMake installation or, as #Fraser pointed out above, make sure to point CMAKE_MODULE_PATH to the directory where it is. Not sure, but it could be that CMake checks the current directory as well automatically (where your CMakeLists.txt resides. Anyhow, setting EIGEN3_INCLUDE_DIR is totally unrelated to the location of FindEigen3.cmake
However, it could be that your FindEigen3 script evaluates this variable to determine the location of your Eigen3 installation.
Alternatively, self-built CMake-based projects often provide a <PackageName>Config.cmake. If you point a variable called <PackageName>_DIR to the directory containing this file, you can use FIND_PACKAGE( <PackageName> ...) as normal. See documentation of FIND_PACKAGE for details.
Eigen >= 3.3 has direct CMake integration, so it seems yours isn't set up correctly.
Assume Eigen has been installed to /opt/eigen/3.3 with default settings. Append or prepend the the location /opt/eigen/3.3 to the environment variable CMAKE_PREFIX_PATH e.g in bash:
export CMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH:/opt/eigen/3.3"
Then CMake should pick it up.
If you are writing your own CMakeLists.txt that uses Eigen I suggest using code like this:
find_package (Eigen3 3.3 REQUIRED)
add_executable (example example.cpp)
target_link_libraries (example Eigen3::Eigen)
You should not use include_directories since CMake 3 was released -- the targets approach should be preferred whenever available.
First, make sure Eigen is properly installed. Refer to the INSTALL file that comes with the tarball.
Second, copy the cmake/FindEigen3.cmake file from the tarball to the directory containing your CMakeLists.txt.
In your CMakeLists.txt add:
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
Now you should be able to do e.g. #include <Eigen/Core>.
All of this comes (mostly) from this source.
This approach has the advantage over e.g. include_directories("$ENV{EIGEN3_INCLUDE_DIR}") that it the uses CMake's standard mechanism for finding external dependencies, making it easier for someone else (or your future self) to pick up the project, possibly on another platform.
(However, it would be nice if Eigen itself installed an EigenConfig.cmake file, making it accessible through the find_package mechanism without any extra paths.)
I found another solution here (which referred to here) which uses the pkg-config file :
find_package(PkgConfig)
pkg_search_module(Eigen3 REQUIRED eigen3)
As a detailed explanation of the note 7 from the top answer of Johannes. By using the "Config mode" rather than "Module mode" of CMake find_packate(), only writing
find_package( Eigen3 REQUIRED )
include_directories( EIGEN3_INCLUDE_DIR )
in CMakeLists.txt is enough.
Please refer to the INSTALL guidance text file contained in the eigen source code directory (eg. extracted from eigen-3.3.7.tar.gz downloaded from official website), which said:
Method 2. Installing using CMake
********************************
Let's call this directory 'source_dir' (where this INSTALL file is).
Before starting, create another directory which we will call 'build_dir'.
Do:
cd build_dir
cmake source_dir
make install
The "make install" step may require administrator privileges.
You can adjust the installation destination (the "prefix")
by passing the -DCMAKE_INSTALL_PREFIX=myprefix option to cmake, as is
explained in the message that cmake prints at the end.
Just like installing usual CMake projects to your machine.
The difference from directly including the /usr/include/eigne3 or /usr/local/include/eigen3 directory is that, a configuration directory called eigen3/cmake will also be installed to /usr/share or usr/local/share. This configuration directory contains the Eigen3Config.cmake file, which can automatically be found by CMake to locate the eigen3 directory.
If you have not added Eigen Library to environment variable then do the following in CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(ProjectName)
set(CMAKE_CXX_STANDARD 11)
# set EIGEN_DIR variable to Eigen Library Path
set(EIGEN_DIR "C:\\Eigendir\\Eigen")
# include the directory
include_directories(${EIGEN_DIR})
add_executable(ProjectName main.cpp)
target_link_libraries(ProjectName ${EIGEN_DIR})
Another simple way that doesn't require adding an environment variable is to simply find eigen with the cmake find_path function https://cmake.org/cmake/help/v3.6/command/find_path.html. The example code finds the directory "Eigen" in paths "/usr/include/" and "/usr/local/include" with an additional search in subdirectory "eigen3".
find_path(EIGEN3_INCLUDE_DIRS "Eigen" paths "/usr/include" "/usr/local/include" path_suffixes "eigen3")
message(${EIGEN3_INCLUDE_DIRS})
if ( NOT EIGEN3_INCLUDE_DIRS )
message(FATAL_ERROR "CMake variable EIGEN3_INCLUDE_DIRS not found.")
endif()
include_directories ( "${EIGEN3_INCLUDE_DIRS}" )
You could try setting the CMAKE_MODULE_PATH to the location of Eigen subdirectory named "cmake":
cmake . -DCMAKE_MODULE_PATH=<Eigen root dir>/cmake/
I had a similar problem when run cmake .. && make on Google Colab on a project clone from github. I fixed it by editing CMakeLists.txt file and adding this line to it:
set(EIGEN3_INCLUDE_DIR "/usr/include/eigen3")
As you can see, my eigen3 was in /usr/include/eigen3 path (and I had installed it using !sudo apt-get install build-essential cmake libeigen3-dev)
When installing on Kubuntu 20.04 following steps from INSTALL:
Do:
cd build_dir
cmake source_dir
make install
using -DCMAKE_INSTALL_PREFIX=/usr/local/eigen-3.4.0 and sudo
FindEigen3.cmake is not installed. However, using
set(Eigen3_DIR "/usr/local/eigen/share/eigen3/cmake")
seem to work. FindEigen3.cmake is in the build directory
but it is not copied to the installation directory tree.
This worked:
Changed c++11 to c++14
Changed find_package(Eigen3 QUIET) to find_package(Eigen3 CONFIG)
Another simple way is:
if the Eigen library is in /opt/eigen/3.3/Eigen
include_directories(/opt/eigen/3.3)
which is equivalent to what Jai suggested
set(EIGEN_DIR "/opt/eigen/3.3")
include_directories(${EIGEN_DIR})
You need to read official documentation:
Click here
After install Eigen from tar and compile it correctly.
CMakeLists.txt will looks like this:
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
add_executable(${PROJECT_NAME} something.cpp)
target_link_libraries(${PROJECT_NAME} Eigen3::Eigen)
It will be fine work.