Using cmake to compile glfw on windows - c++

I didn't know how to title this, it's quite unaccurate. Anyway here is the question:
I want to create a CMake project in Visual Studio that has to use glfw. However, I don't want to use the precompiled binaries, I want to compile glfw along with my application using the CMakeLists.txt file.
Here's my CMake file:
# CMakeList.txt : CMake project for BasicMandelbrotAnimation, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project(BasicMandelbrotAnimation)
# Set the main file to main.cpp
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")
# Include our different directories
include_directories(include)
include_directories(source)
# Include all source files in source directory
file(GLOB SOURCE source/*)
# Compile GLFW
add_subdirectory(extlibs/GLFW)
# CMake GLFW Settings
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
# Add source to this project's executable.
add_executable (${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} glfw)
find_package(OpenGL REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${OPENGL_gl_LIBRARY})
target_link_libraries(${PROJECT_NAME} ${OPENGL_glu_LIBRARY})
# TODO: Add tests and install targets if needed.
The error I get is "Cannot open include file: 'unistd.h': No such file or directory" which is bugging me out because this is a UNIX file so how can I adapt the files to compile in windows?
Thank you.
EDIT:
This questions seemed like a duplicate, but isn't really. unistd.h is only the first problem, let's have a look at the problematic file:
#ifndef _glfw3_x11_platform_h_
#define _glfw3_x11_platform_h_
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xcursor/Xcursor.h>
// The XRandR extension provides mode setting and gamma control
#include <X11/extensions/Xrandr.h>
// The Xkb extension provides improved keyboard support
#include <X11/XKBlib.h>
// The Xinerama extension provides legacy monitor indices
#include <X11/extensions/Xinerama.h>
#if defined(_GLFW_HAS_XF86VM)
// The Xf86VidMode extension provides fallback gamma control
#include <X11/extensions/xf86vmode.h>
#endif
Here it is, first of all, unistdh is missing, I replace it with what was proposed in another post, but now it's the others that are missing, specifically dlfcn.h and X11 files and I don't know how to fix that.
Thank you.
EDIT2:
Forget what's inside x11_platform.h. The problem is in this line:
target_link_libraries(${PROJECT_NAME} glfw)
when doing this, cmake need x11_platform.h, that does not exist on windows. Does someone know how to skip this file or link another way?
Thank you.

Related

Unabled to load lohmann/json.hpp using CMake - getting fatal error: 'nlohmann/json.hpp' file not found

I have the following main.cpp, very simple script, trying to re-produce the problem and isolate to it's most basic.
#include<iostream>
#include<fmt/core.h>
// #include "json/json.hpp"
// #include <json/json.hpp>
// #include <nlohmann/json.hpp>
// #include "json.hpp"
// #include "nlohmann/json.hpp"
int main(){
fmt::print("Hello, world!\n");
return 0;
}
The commented out include statements are all of the paths I have tried to get this into my program.
My CMakeLists.txt file looks like this
cmake_minimum_required(VERSION 3.24)
project(main)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-std=c++17")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
# include(FetchContent)
# FetchContent_Declare(
# json
# GIT_REPOSITORY https://github.com/nlohmann/json
# )
# FetchContent_MakeAvailable(json)
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG 9.0.0
)
FetchContent_MakeAvailable(fmt)
add_executable(main main.cpp)
# target_link_libraries(main json)
target_link_libraries(main fmt)
I've commented out the json part since it's not working obviously, I can also json-src and json-build in my _deps folder so I know for a fact that it is being downloaded into my local machine.
After running cmake CMakeLists.txt and make and then running the executable I get the error in the title 'nlohmann/json.hpp' file not found
Any help would be appreciated.
Tried to reproduce the example with another popular C++ package which worked fine. Tried changing the include statements to see if maybe I had something wrong since I don't fully understand all the nuance with CMake and am quite new to it.
You can either specifically use the interface target already included in the nlohmann library, which will automatically populate the correct include path for you, with:
target_link_libraries(main nlohmann_json::nlohmann_json)
Or you would need to specifically include the include path yourself:
include_directories(${json_SOURCE_DIR}/include)
With either way, you will be able to use #include <nlohmann/json.hpp> in your source.

CMake bracket #include grpc

I am trying to integrate grpc with a large project by trying to integrate the code in grpc example helloworld directory: greeter_client.cc
I used the cmake option where assuming grpc is already installed in my system in $MY_INSTALL_DIR as described in grpc's document: cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR
When I include it in my "large project", I changed the greeter_client.cc into .h and .cc file.
greeter_client.h
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
class GreeterClient {
public:
GreeterClient(std::shared_ptr<Channel> channel)
: stub_(Greeter::NewStub(channel)) {}
std::string SayHello(const std::string& user);
private:
std::unique_ptr<Greeter::Stub> stub_;
};
void libFun();
greeter_client.cc
#include <iostream>
#include <memory>
#include <string>
#include "greeter_client.h"
std::string GreeterClient::SayHello(const std::string& user) {
// the original implementation unchanged...
}
void libFun() {
GreeterClient client(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
client.SayHello("world");
}
and in the target destination (say the file is main.cc) I added
#include "/path/to/greeter_cient.h"
int main (){
libFun();
return 0;
}
The directory structure is
helloworld
greeter_client.h
greeter_client.cc
CMakeLists (the original one with little tweak to also include greeter_client.h in target greeter_client)
protos (the protos directory under examples in grpc project)
test
main.cc
CmakeList
The CMakeList for main.cc is like the following
cmake_minimum_required(VERSION 3.5.1)
project(test C CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# This part is copied from the grpc example CMakeFileList
set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
add_subdirectory("../helloworld" "../helloworld")
add_executable(program "main.cc")
# THIS IS THE QUESTION: why adding ${_GRPC_GRPCPP} will work?
target_link_libraries(program greeter_client
${_GRPC_GRPCPP})
target_include_directories(program PRIVATE "../helloworld" )
The question is: initially, I did not add find_project(gRPC) in the main.cc 's CMakeList and I did not add ${_GRPC_GRPCPP} in the target_link_libraries of program target. And I will get error
complaining "/test/../helloworld/greeter_client.h:23:10: fatal error: 'grpcpp/grpcpp.h' file not found
#include <grpcpp/grpcpp.h>"
I read some threads and it seems that bracket include finds the files in system include paths. I am not sure why the grpc example can make it work by adding a dependency in target_link_libraries?
Another question is: ideally, I want main.cc does not care about anything inside greeter_client target. However, since I have to include greeter_client.h which in turn includes <grpcpp/grpcpp.h> I have to add the find_project and link the target in main.cc's CMakeList as well. How to avoid re-deal with this include <grpcpp/grpcpp.h> in main.cc's CMakeList?
Thanks!
If I understand you correctly, the real question here is
it seems that bracket include finds the files in system include paths.
I am not sure why the grpc example can make it work by adding a
dependency in target_link_libraries?
In short, #include <foo.h> will search system include paths first, but if that fails it will retry as if you wrote #include "foo.h".

CMake: can't find my header file in a different directory

I have a CMake project with these relevant folders:
project_folder
-src
|--main.c
peripherals
|--something.h
|--something.c
My CMake in project_folder includes:
add_subdirectory(peripherals)
if (NOT $ENV{TARGET_SOURCES} STREQUAL "")
target_sources(app PRIVATE $ENV{TARGET_SOURCES})
else()
target_sources(app PRIVATE src/main.c)
endif()
My CMake under peripherals incudes:
add_library (peripherals something.c)
target_include_directories (peripherals PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
CMake under src:
add_executable(app main.c)
target_link_libraries(app PRIVATE peripherals)
My project is building fully but when I try to include my header file in main.c I get:
project_folder/peripherals/something.h:No such file or directory
In my main.c file I have #include "peripherals/something.h". Does anyone know how to fix this? I'm not sure if my #include statement is correct and I think I am missing stuff in my CMakeLists files
You can either do "#include "../peripherals/i2c_test.h" in your main.cpp
OR
in your CMake in project_folder:
target_include_directories(app ${CMAKE_CURRENT_SOURCE_DIR})
and then in main.c:
#include <peripherals/i2c_test.h>
....
You'll need to define executable and "link" it with the library:
# project_folder/CMakeLists.txt
add_subdirectory(peripherals)
add_subdirectory(src)
# project_folder/src/CMakeLists.txt
add_executable(my_executable main.c)
target_link_libraries(my_executable PRIVATE peripherals)
Then, you'll need to include the header in main.c properly- since you've linked against a library that includes peripherals directory, you can now directly include it:
#include "something.h"
Your include statement has to be relative to the included directory.
Edit: In your example ${CMAKE_CURRENT_SOURCE_DIR} is the project directory (where your CMake file is).
Therefore you should be able to write:
#include <peripherals/something.h>
You can always check a cmake variable's content by printing it. In your CMake file you can write:
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
When running cmake you should see the path printed in the console output.
in your CMake in project_folder:
include_directories(src)
and then in main.c:
#include <peripherals/i2c_test.h>
OR
in your CMake in project_folder:
include_directories(src/peripherals)
and then in main.c:
#include <i2c_test.h>

Cannot get cmake to link to GLFW library

I am trying to write an OpenGL application with GLFW. The file structure is as follows
-USG
-build
-include
-glfw-3.3
-src
-CMakeLists.txt
-main.cpp
-CMakeLists.txt
The CMake in the USG file is as follows.
cmake_minimum_required(VERSION 3.0)
project(USG)
#--------------------------------------------------------------------
# Set GLFW variables
#--------------------------------------------------------------------
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
#--------------------------------------------------------------------
# Add subdirectories
#--------------------------------------------------------------------
add_subdirectory(include/glfw-3.3)
add_subdirectory(src)
And the CMakeLists in the src file is as follows
add_executable(usg main.cpp)
set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED)
if (OPENGL_FOUND)
target_include_directories(usg PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(usg ${OPENGL_gl_LIBRARY})
endif()
target_link_libraries(usg glfw)
I run cmake ../ followed by make in the build folder. Running cmake runs as expected. Running make builds glfw fine but when it gets to main.cpp it fails at #include <GLFW\glfw3.h> telling me GLFW\glfw3.h: No such file or directory. It seems to me like the cmake isn't properly linking the header file to the main.cpp file, but I don't know enough about cmake to get that to work. I've tried looking through the examples provided in the glfw-3.3 example folder but I couldn't make enough sense of it to solve my problem. I've tried drawing what knowledge I could from the many similar problems on stack overflow but none of them could help.
I'm following this tutorial here. I want the OpenGL application to be relatively portable, which is why I'm compiling glfw from source rather than using a binary. I'm building on Linux, if that matters.
Try #include <GLFW/glfw3.h> instead of #include <GLFW\glfw3.h>
Backslashes in include paths are a bad (Windows)-practice and do not work with GCC.
https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html

Why does Clion not detect standard library headers when using Cmake?

I have a project in Clion using CMake and C++14. The project compiles but all standard library includes are marked as:
"Cannot find string", "Cannot find stdexcept", etc.
Additionally the symbols from the dll I included are not being detected. So they are all marked as:
"Cannot resolve ..."
I've included the header and cmakelist.txt. This is only happening in this project and I have almost identical cmakelist.txt files for all my projects. I have tried restarting CLion's cache. I also tried moving all the files to a new project which worked momentarily but with an hour CLion was flagging these lines again.
cmakelists.txt
cmake_minimum_required(VERSION 3.6)
project(BCI)
set(CMAKE_CXX_STANDARD 14)
#create dlls and executables in the root directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
include_directories(
${CMAKE_SOURCE_DIR}
)
set(SOURCE_FILES
NeuralInterface.hpp
)
add_library(BCI SHARED ${SOURCE_FILES})
set_target_properties(BCI PROPERTIES LINKER_LANGUAGE CXX)
NeuralInterface.hpp
#ifndef NEURALINTERFACE_HPP
#define NEURALINTERFACE_HPP
//c++ includes
#include <stdexcept> //these are the includes which cannnot be resolved
#include <string>
//project includes
#include "okFrontPanelDLL.h"
extern std::string IntanAcquire; //this says cannot resolve container std
...
#endif
What else can I do to CMake so it finds these headers?