C++, setting up include with Cmake - c++

I want to use the ICU package and I tried installing with and without vcpkg, and with vcpkg I do not get a directory for where it was installed like I see in tutorials, so I had to manually find it nor did I get a Cmake toolchain paste line from installing with vcpkg. As for the Cmake file, I try following this tutorial
https://www.youtube.com/watch?v=FeBzSYiWkEU
but when I go to the configuration manager in Visual Studio I am brought to a json file with no option to change the toolchain, and when I try to go to the settings json file in Visual Studio Code instead, I instead find three different settings json files, none of which have the toolchain file option to change like with Visual Studio. I have had similar difficulties with the Cmake file when trying to install other packages for C++ and have no idea what I am missing from tutorials that seems to no matter what have the Cmakefile.txt never work for me.
I would like to just be able to use the ICU package without having to use Visual Studio and just be able to statically link the package with my .cpp file in the same folder that VS Code uses, and just g++ compile from cmd, however I am in over my head every time I try to get the CMake file to work. If all I know for sure is that I have the latest ICU package downloaded with vcpkg, how would I go about setting up the CMake file?

cmake_minimum_required(VERSION 3.24)
project(MyICUProject)
find_package(ICU REQUIRED)
add_library(MyLibUsingICU lib.cpp)
target_link_libraries(MyLibUsingICU PRIVATE ICU::i18n) # or whatever you use from icu. check https://cmake.org/cmake/help/latest/module/FindICU.html
have a manifest (vcpkg.json) along the sources
{
"name": "MyICUProject",
"description": "<description>",
"dependencies": ["icu"]
}
Invoke with (if you are in the source dir):
cmake -G "Ninja" -B "build" -S . -DCMAKE_TOOLCHAIN_FILE=<path_to_vcpkg_toolchain>/vcpkg.cmake

Related

Visual Studio's CMake with vcpkg: Error gdal is not found

I have been trying to build a cmake c++ project. More specifically I am trying to use the gdal library in this project. In the CMakeLists.txt it says find_library(GDAL gdal) after doing some research i found, that visual studio can open cmake files by default as mention in this thread: https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=vs-2019.
Moreover, visual studio should also automatically include the gdal library once i have set it up with vcpkg correctly. I've already downloaded the x64-windows version of the library (vcpkg install gdal:x64-windows) in order to build for the right architecture and made it available via vcpkg integrate install on a user-wide scope.
After some trial and error, everything works fine now, the toolchain gets included accordingly and the library is found automatically, resulting in a configuration like that:
However, when trying to include the header files (or anything else; see code snippet), visual studio does not seem to link the library correctly as it will result in the error message: cannot open source file "gdal/gdal.h".
#include <gdal/ogrsf_frmts.h>
#include <gdal/gdal.h>
#include <gdal>
Where should I further investigate?
As others have said vcpkg integrate install and vcpkg.cmake don't work together the reason being:
set_target_properties(${name} PROPERTIES VS_USER_PROPS do_not_import_user.props)
set_target_properties(${name} PROPERTIES VS_GLOBAL_VcpkgEnabled false)
this deactivates the integration. The reason to deactivate the integration is so that you don't write an incomplete CMakeLists.txt (e.g. missing the include directory or not linking all required libraries).
As such replace find_library(GDAL gdal) with find_package(GDAL REQUIRED) and target_link_libraries against the target GDAL::GDAL (https://cmake.org/cmake/help/v3.17/module/FindGDAL.html)

"CMakeLists.txt" seems to be missing

I was trying to build SFML in order to start developing 2d-games in c++. I put the source-code directory inside C: (the directory itself is called 'SFML'). while trying to generate .cbp files using CMake, I ran into an error:
"The source directory "C:/SFML" does not appear to contain CMakeLists.txt"
It seems like there is a missing file that CMake didn't create.
I am using Cmake 3.10.1, the installation file can be found in https://cmake.org/download/ under the title:"Windows win32-x86 Installer".
I tried to build SFML 2.4.2, using the generator of Codeblocks - MinGW makefiles. The version of codeblocks used is: codeblocks-16.01mingw-setup.exe, found in http://www.codeblocks.org/downloads/26
Did someone encounter the same problem before and knows how to overcome it? If not -
Is there an already-built, ready to use, libray of this version? (2.4.2)
If not.. do you know about a version of SFML that is usable in visual studio 2017?
And another question (That is the last one, I promise...) - maybe you can recommend me about another library for c++, that is usable to develop 2d-games?
edt:
note: I am completely new to all this building thing
the containments of SFML:
maybe it's the binary version?
lib contains some static libraries, bin contains some .dll s
You probably downloaded something wrong or potentially extracted the SFML source into a sub folder or something like that.
Since you've mentioned Visual Studio: SFML is 100% compatible with Visual Studio and you can build it from source any time without having to worry about dependencies (everything included in the repository).
The steps to compile are pretty easy. I'd recommend you install Git to clone the latest source from the SFML repository. In addition you'll need CMake, which you obviously got already.
Open a Command Prompt and change your directory to C:\SFML.
Clone the official repository using Git: git clone https://github.com/SFML/SFML.git source (note the trailing .)
Wait for the source to be checked out to the source sub directory.
Create a new directory called build: md build && cd build
Now invoke CMake, pointing to the source directory and also defining your install path: cmake -DCMAKE_INSTALL_PREFIX=C:/SFML ../source
Build and install a debug build of SFML: cmake --build . --target install --config debug
Build and install a release build of SFML: cmake --build . --target install --config release
Alternatively you can just open the generated Visual Studio solution and build the INSTALL project on your own.
(You shouldn't have to define a build tool or anything; CMake should notice Visual Studio being installed and pick it up automatically.)
I have experienced this using cmake version 3.10 when using the -S command line option. On cmake version 3.15.4 it behaves as expected - so if you update cmake then it might just work.
Note that the -S command line option was not supported before version 3.13:
https://cmake.org/cmake/help/v3.13/release/3.13.html#command-line
Normally cmake is called from a "build" subdirectory of a project root, the project root contains CMakelists.txt. So you would create a "build" directory, change to it, and submit "cmake ..". This would pull in CMakelists.txt from the project root, and use the "build" subdirectory that you are in as the build directory. We say "build", what it actually does is generate makefiles which can then used by the "native" build system (e.g. "make" or Visual Studio) to perform the actual compilation and linking. This arrangement is not always convenient, hence the introduction of the -S and -B command line options.

Compiling MariaDB C/C++ Connector on Windows

I am trying to use MariaDB in my visual studio project, so after looking at the documentation, I installed MariaDB and downloaded the source code for the C/C++ connector.
I got the include folder linked to the project, so I am able to make calls to the functions there, but it won't build because the linker fails to find the function definitions (obviously). I have looked all over and cannot find any solid directions on building the .lib files I need. I have found two sources that say that I need to just build the mariadb_connector_c.sln, but that file does not exist in the source code that I got, nor does it exist on their Git repo: https://github.com/MariaDB/mariadb-connector-c
The closest thing that I have found is the appveyor.yml file they provided, which contains the following: Contents of YML File
Is there anybody out there who has done this before? I can't find anything online or in the files they provided pertaining to this.
UPDATE:
I found the answer thanks to some of the replies. I needed to run the source code through cmake in order to get the .sln that I could then build to get the .lib files I need to link to the project.
The answer is a generic answer for any cmake project
Install CMake. make sure cmake.exe is in PATH (Windows installer offers this option). Install Visual Studio, and make sure C/C++ compiler is installed
start command line prompt
Execute "cmake . && cmake --build ." in the command line prompt.
That#s it.

Using cmake on windows for c++

for around 5 consecutive days i have been trying to set up my computer with the c++ environment for programming with libraries such as sdl,glm,opengl. its important for us to be able to run it on unix machines on presentations so im running with cmake.
i finally got it to work with the cmake-gui, i wont even bother trying anymore with any IDE.
i specified my folder project and where to build the binaries, i got a folder "CMakeFiles" along with a txt "CMakeCache", a CMAKE file "cmake_install.cmake" and a file "Makefile". also in my folder "CMakeFiles" there are lots of other folders such as "CMakeTmp", "CompilerIdC", "CompilerIdCXX etc" and in both folders "Compiler*" has each an .exe which doesnt work! so where is my wanted executable?
i opened cmd and navigated to my folder and tried to write "make" as we are supposed to do according to the intruction. alas, it didnt work very well. hoping you could share your wisdom and help a newbie like me!
so what exactly is needed for compiling projects containing additional libraries? so far i have a compiler, Mingw32, the latest CMake and using the cmake-gui for extracting the binaries but gets makefiles.
EDIT:
hrrm. is it only me who gets these kind of problems? i can add that i have look thorough about 10 tutorials and 90% of the steps are similar (if compiling with VS which i tried at first):
Download latest SDL
Make a folder on e.g C:\SDL with two folders, include and lib
Copy the libs and includes from the downloaded SDL
Make new VS project, open VC++ directories and add lib/incl folder on e.g C:\SDL
Add to linker SDL.lib and SDLmain.lib (i made sure they got linked, no problem here)
Change system to WINDOWS (optional if you dont want two windows)
Added include to "additional libraries"
Put the SDL.dll file (which i got from the latest SDL) in my C:\windows\system32(64SysWoW)
and also in my project file.
so what i am actually looking for is gettning the CMake to work, since it generates and builds sources successfully (with the gui) and i feel im closing in. do i need to add any additional libraries from sdl to my compiler mingw32 and/or cmake?
if you run cmake by command:
cmake -G "Visual Studio 14 Win64" path\to\source\dir
you need to run this command to continue(in Visual Studio Command Prompt):
msbuild Project.sln
either if you run cmake:
cmake -G "NMake Makefiles" path\to\source\dir
you need to run this cmd to continue(in Visual Studio Command Prompt):
nmake
You were almost there with Visual Studio. Select Visual Studio as target. Open the generated project in Visual Studio, build it. (just like you alread did). Then, instead of trying to run BUILD_ALL, run a real project that creates an executable, it should also be in that list. Just right click it and 'play' it.
If you still get errors, post them in detail including what you did before the error. Note: a carefully configured cross platform CMake project (aka the CMakeLists.txt) should not require any fiddling with VC++ directories. It should work automagically, especially with well known libs such as SDL.
If I understood it correctly you want to use CMake in your project. I'm using CMake in all my projects. I won't give you exact step-by-step howto, since I use Arch Linux but I used it in Windows 7 too.
To make CMake find the libraries, it is often needed to set up the CMAKE_PREFIX_PATH environment variable so it points to the directories where dependencies of your project are installed.
Set you PATH environment varible so you can invoke you compiler and make just by calling by calling eg. make. I think you need to do than manually for Mingw32, for Visual Studio you can use the "Visual Studio Command Propt" which has these variables already set.
Run CMake with desired generator. To select the generator from command line use the -G switch. You will probably use one of the following (the ... means other options you want to pass to cmake)
For GNU make used in MinGW use cmake -G "MinGW Makefiles" ...
For NMake from visual studio use cmake -G "NMake Makefiles" ...
It is also possible to create a Visual Studio project but I do not recommend it, since it quite difficult to set up automatic builds then. I also had some problems with dependencies when I tried to use VS project.
change directory to your build directory (ie. the one where you called cmake, it contains the CMakeCache file) and run make or nmake
Quoting from "CMake support in Visual Studio":
Visual Studio 2017 introduces built-in support for handling CMake projects. This makes it a lot simpler to develop C++ projects built with CMake without the need to generate VS projects and solutions from the command line. This post gives you an overview of the CMake support, how to easily get started and stay productive in Visual Studio.

How to add files to Eclipse CDT project with CMake?

I'm having problem getting the source and header files added into my Eclipse CDT project with CMake. In my test project (which generates and builds fine) I have the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(WINCA)
file(GLOB WINCA_SRC_BASE "${WINCA_SOURCE_DIR}/src/*.cpp")
file(GLOB WINCA_SRC_HPP_BASE "${WINCA_SOURCE_DIR}/inc/*.hpp")
add_library(WINCABase ${WINCA_SRC_BASE} ${WINCA_SRC_HPP_BASE})
This works fine but the resulting Eclipse project files contains no links to the source or header files. Anyone knows why? Are there any other cmake command I have to use to actually add the files into the project?
I realize it's been a while since you've post this, but fwiw, it work's for me fine with CMake 2.6 or 2.7 (trunk) versions, generating for Eclipse/Ganymede. What I do is first run
cmake -G "Eclipse CDT4 - Unix Makefiles" /path/to/src
which generates the Eclipse project files as well as the makefiles, then "Import Project" in Eclipse.
Works beautifully...
sly
I use CMake 2.4, not 2.6 but in 2.4 they specifically warn against using GLOBs to find the files to build.
This is because it will notice if new files are added or deleted, so it will not be able to figure out the dependencies.
If you have to explicitly add the files to your CMakeLists.txt then this file will be newer than the makefiles and the cache files. So CMake will know to regenerate them.
If the files are added with a glob no files CMake knows about change with you add new files so CMake doesn't know that it has to regenerate the makefiles etc. This is the same for regular makefiles and Visual Studio projects.
Unless the CMake 2.6 docs explicitly says it is ok to add files like this I would avoid it. It is not that hard to manage the source files in cmake. How often do you add new files?
The problem I had was I made an "in-source" build instead of an "out-of-source" build. Now it works fine, and it was actually lots of info on this on the Wiki but somehow I misunderstood it.