CMake compile cpp project for windows - c++

Good afternoon!
I have a C ++ project in Macos with CLion, it works perfectly but when I compile it only creates the files prepared for Mac (or so I think). I want to transfer the project to a windows so that my colleagues can use it without having a mac. Is it possible to configure the CMakeList so that a file is created that can be used in windows?
I give you the configuration that I have in CMakeList
cmake_minimum_required(VERSION 3.19)
project(serviceToSQL)
set(CMAKE_CXX_STANDARD 14)
add_executable(serviceToSQL main.cpp tinyxml2.cpp)
add_library(tinyxml2 tinyxml2.cpp tinyxml2.h)
add_library(tinyxml2::tinyxml2 ALIAS tinyxml2)
Thank you!

Related

Why doesn't visual studio find soundio.h?

I've been working with visual studio 19 and cmake. The initial objective is to link spdlog and libsoundio libraries to my project, to achieve it the following CMakeFile was created to compile my project.
cmake_minimum_required (VERSION 3.8)
set(APPNAME DrumHero)
project($APPNAME)
#C++ version
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCES src/drumhero.cpp)
set(HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/drumhero.h)
set(DEPENDENCIES_LIBS spdlogd libsoundio)
link_directories("C:/Program\ Files/libsoundio/lib" "C:/Program\ Files/spdlog/lib")
include_directories("C:/Program\ Files/libsoundio/include" "C:/Program\ Files/spdlog/include")
add_executable (${APPNAME} ${SOURCES} ${HEADERS})
target_link_libraries(${APPNAME}
PUBLIC ${DEPENDENCIES_LIBS})
set_target_properties(${APPNAME} PROPERTIES
PUBLIC_HEADER "${HEADERS}")
All libraries are installed and located in Program Files.
I copied the example program from libsoundio, but visual studio is showing some errors related to libsoundio, it seems that the library is not found by visualstudio, like the errors shown in the image below:
What it's most strange is that the code compiles, but when the .exe is executed the following message is shown:
Translation:
The execution of the code could not continue because libsoundio.dll was not found. Reinstalling the program to fix the problem.
Why is libsoundio not found?
If your code has sucessfully compiled, you have two options: Either copy the libsoundio.dll in the same folder as your .exe, or else, copy that DLL into a typical Windows path for DLLs, for example, C:\Windows\System32.
The first method should be easier if you want to deploy your application to another computer. That is the reason why Software for Windows is distributed as Installers, which create a folder in your machine and copy multiple files, sometimes DLLs, in the same path as the .EXE

Compiling a 32-bit C++ program in CLion on MacOS

I having trouble with my CMake file in CLion running on Mac OSX 10.14. Currently, I have the following for a 32-bit program I copied from a Linux server using a built "supplied.o" file from an instructor. I wanted to work in CLion and wondering if it is possible to add to CLion to work outside of the Linux server?
cmake_minimum_required(VERSION 3.14)
project(app)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(app
dlist.cpp
dlist.h
main.cpp
supplied.o)
set_target_properties(app PROPERTIES LINKER_LANGUAGE CXX )
set_target_properties(app PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
I tried poking around here to see if I could figure it out, but it's possible I don't have a 32-bit G++ library (is that i386?) and not quite sure how to go about installing that. I have homebrew and feel comfortable enough with Linux to just work there, but I enjoy CLion for getting quick ideas out. Thanks for any help anyone can provide!

Enabling curses in both Linux and Windows

I am working on small project in C++ and I am using curses for user interface. I am pretty nicely able to make it work in my arch-linux installation, because it is pretty simple to set up ncurses to work there. But with my cmake setting which is working nicely at Linux I am not able to properly make it work at Windows.
Here is my CMakeList.txt
cmake_minimum_required(VERSION 3.9)
project(fighting_pit)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_CXX_STANDARD 11)
include_directories( ./include)
include_directories( ./src)
add_executable(fighting_pit
include/Arena.h
include/cursor.h
include/Player.h
include/spell.h
include/Turns.h
include/weapon.h
include/Draw.h
src/Arena.cpp
src/cursor.cpp
src/Player.cpp
src/spell.cpp
src/Turns.cpp
src/weapon.cpp
src/Draw.cpp
main.cpp )
target_link_libraries(fighting_pit ${CURSES_LIBRARIES})
I tried several approaches to make it work on Windows too.
1. Downloading sources
I tried to build pdcurses with mingw32-make. It created pdcurses.a I added it to same location as project, but it still shows it cannot find curses library.
2. Downloading via mingw32-get
I used installation manager from mingw and let it download both .dll and dev package of libpdcurses. Just trying to run cmake through clion showed it is still not found. So I copied it both into windows32 and project folder but it still didn't help.
I don't know what I should do. Unfortunately I am neither C++ user neither Windows user.
I needed to build a cross-platform project that uses ncurses on Linux and MacOS but uses pdcurses on Windows. Some variant of curses is usually installed on popular distributions of Linux. ncurses is also available on MacOS as well. The same isn't quite true for Windows. My solution was to download the pdcurses sources and write a cmake script for building it on Windows. if (WIN32 or MSVC) build pdcurses else() find ncurses. You might also want to create a proxy header that #includes pdcurses or ncurses depending on the platform.
After cloning the GitHub repo, I copied the headers in ., the C files in ./pdcurses, the sources in ./wincon into a new directory in my project. Then I wrote a CMakeLists.txt file to compile all of these files into a library. It looked something like this:
cmake_minimum_required(VERSION 3.2)
add_library(PDcurses
# all of the sources
addch.c
addchstr.c
addstr.c
attr.c
beep.c
# ...
)
target_include_directories(PDcurses
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
My main CMakeLists.txt file compiled the pdcurses sources if the target is windows.
if(WIN32 OR MSVC)
add_subdirectory(pdcurses)
target_link_libraries(MyTarget
PRIVATE
PDcurses
)
else()
# find ncurses and use that
endif()
PDCurses seems to be a (more or less) drop-in replacement for ncurses in most situations. I was able to compile the example programs that came with PDcurses on my Mac using curses without any troubles.

CMake on Windows 10 doesn't generate executable

So I have a tiny C++ project that I code on both Windows 10 and Ubuntu 16.04, and I'm using CMake to coordinate relatively platform-independent build processes. I'm no master of CMake, but it worked up until recently, however now I cannot get the solution generated by CMake-gui to actually compile into an executable to run, despite my usage of add_executable. Here is my CMakeLists (there's a hideous mix of platform-specific stuff in there, however it appears that CMake simply ignores it):
cmake_minimum_required(VERSION 3.10)
set (CMAKE_CXX_STANDARD 17)
project (TestProject)
include_directories(C:/include/CImg-2.2.2)
ADD_LIBRARY(LibsModule Game.cpp Game.h TestGame.cpp TestGame.h)
target_link_libraries(LibsModule -lpthread)
target_link_libraries(LibsModule -lm)
target_link_libraries(LibsModule -lX11)
add_executable(TestProject TestGame.h)
target_link_libraries(TestProject LibsModule)
This compiles and runs fine on Ubuntu using CLion, however on 64-bit Windows 10 using VS17 it fails upon trying to run the executable it expects to be generated because the executable doesn't exist in the build folder (but the .lib file noted is generated just fine).
I've seen some mention that a third party antivirus program could cause this, but I have no such thing installed.
Now I'm pretty new to CMake so if this file is completely off the mark I would really appreciate a clear explanation as to what I'm doing wrong, this code is made from a short study of basic CMake usage.

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl