Building gtest for visual studio using cmake - c++

I'm really new to cmake, I'm using it because I want to build gtest for multiple versions of VS at the same time. I havent been able to generate anything using cmake yet.. maybe its overkill for my application?
How can I build gtest for multiple versions of VS using cmake, either at the same time, or using some kind of parameters?.

Something which may help would be to build gtest as part of your main CMake build. That way, gtest will always use the same compiler and settings as your main build and you then shouldn't need to pre-build it for each of the different compilers you want to support. Making gtest part of your build also has the advantage that it's really easy to link your tests against gtest, since CMake already knows about the gtest library target, so you don't have to use find_package(GTtest) or tell CMake where to find the gtest libraries.
I recently wrote a blog article about how to do this which may help you. You can find it here:
https://crascit.com/2015/07/25/cmake-gtest/
Update: This approach is now also part of the googletest documentation.

Related

How to setup complex project with Visual Studio 2022?

I have project that I was developing for years in Linux.
It depends on MKL, libxml++, GSL and armadillo library.
Its installation structure is done in CMake and project is formed by building a shared library and couple of executables that link to it. There are about 20 classes in the library.
Project structure is:
--src
--executable1.cpp
--executable2.cpp
--mysharedlib
--class1.h
--class1.cpp
--...
My question is how to install and run this code in Visual Studio in Windows.
I never used VS before and am still going through tutorials. I managed to run my code by installing Ubuntu on WSL, but I I'd like a VS solution as it'd be handy to pass to user not familiar with Linux.
I tried opening the project directory with VS, hoping CMAKE would do all the magic, but expectedly it cannot locate the dependent libraries, so I am now going through web looking how to integrate each to VS. I managed to find armadillo and mkl guide, but I am lost on how to link these libraries to my project codes and whether I should abandon its current cmake setup and start building the code structure differently in VS.
Any links to useful VS tutorials and advices how to this are greatly appreciated.
VS does have support for CMake, although I have no idea how well VS integrates CMake. If you're not set on using VS, you might want to look into an IDE that uses CMake at it's core, Clion comes to mind. That being said, when coming from Linux you don't have the (initial) luxury of simply installing all the dependencies via a preinstalled package manager.
In order for CMake to find your dependencies (assuming you've configured them by using find_package()) you should add the sources of your dependencies to your project in a thirdparty folder (name is up to you) and add these dependencies using add_subdirectory() instead. This will compile all your dependencies from source, so you might have to configure these dependencies yourself (look into the documentation of your dependencies on how to build them from source).
Another way is to use a package manager that is available on Windows to download, compile and provide your dependencies to your build tools. vcpkg comes to mind, claiming to integrate well with CMake by providing a toolchain file that you can pass to CMake when building your project. You might even be able to configure VS to automatically pass this toolchain to CMake whenever it's invoked.
From personal experience, there is no need to convert an existing project to the VS project structure. There's plenty of available solutions and tools available on Windows to work with CMake projects. Going with the cross-platform approach should be preferred unless you're only targeting Windows, using VS to it's fullest then might give you some additional quality of life.
If you have more specific questions regarding this, I suggest that you update your original post or to create separate, specific questions regarding the processes involved in setting up an existing CMake project on Windows.

How to improve my script and copy program resources on build?

So I got into a problem where I needed to use linux for a while instead of windows, and figured linux doesn't have Visual Studio. I then also realized that I made my project Visual Studio only, which I don't want.
So I looked up some CMake tutorials and try'ed creating some examples that could be loaded in both Visual Studio and CodeBlocks. When I got that to work, I went and code a CMake script for my actual program by piecing together what I learned and what I found in tutorials.
See here:
cmake_minimum_required(VERSION 2.8.9)
add_subdirectory(libraries/glfw)
project (OpenGLEngine3D)
include_directories(libraries/glfw/include)
include_directories(libraries/glm)
include_directories(libraries/glad/include)
include_directories(libraries/whereami/src)
include_directories(libraries/stb)
file(GLOB SOURCES "src/*.cpp" "src/*.h" "libraries/whereami/src/whereami.c" "libraries/glad/src/glad.c")
add_executable(OpenGLEngine3D ${SOURCES})
target_link_libraries(OpenGLEngine3D glfw)
Its dirty(I think) but it works.
So now my first question is, how to improve my CMake script? What is redundant or done in a poor way?
Now my program also requires some resources(like shaders, textures) which I have stored in a directory along with my CMake script, libraries and c++ files.
So my second question is, how would I tell CMake to the ide/compiler to copy the files in a certain directory to the program build directory(where the compiled binaries are) after compiling?(And have it only do it when the files aren't there ofc.)
Thanks!
I recently came across this article, which might be help you with CMake.
Here are a couple suggestions from me:
You're safe to use newer version of CMake. Currently, it's 3.11. There's no point in sticking to old versions.
Consider listing all your sources in a variable (using set command) instead of using file(GLOB). file(GLOB) will be evaluated only once, when generating build files. If you add new sources, you'll have to re-generate it manually, so it doesn't help too much, and is harder to debug (and really, debugging more complex CMake projects can be painful).
Avoid using include_directories (and link_libraries). Prefer using target_include_directories which works per-target. It allows to express dependencies between targets (when you have more than one - which might happen as your project grows)
You might consider using find_package(GLFW), instead of including GLFW in your project. Edit: actually, CMake doesn't came with find module for GLFW, but you can use an external module for this (e. g. this one) as described here.
Edit:
Example below illustrates the idea behind point 2), assuming that sources are placed in "src/" directory on the same level as CMakeLists.txt file:
set(engine_sources
"src/header_1.hpp"
"src/source_1.cpp"
# and so on for reset of files...
)
add_executable(OpenGLEngine3D ${engine_sources})
As for the second question: that's what file(COPY) command is for. Alternatively, you could just leave assets in source directory, and set working directory in IDE.
Side note: if you choose the second options, there appears to be a way to set this from CMake for Visual Studio, by setting VS_DEBUGGER_WORKING_DIRECTORY property:
set_target_properties(OpenGLEngine3D PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
(I haven't checked if this works as expected, as I rarely use VS. I remember looking for something like that in the past, and just recently came across it.)

Generate Embarcadero project files from CMake files

I have a C++ code base with a large number of projects. Usually I can use CMake to generate the required project files for QtCreator, XCode, Visual Studio, etc. Now I need to compile for Embarcadero, but it does not seem that CMake supports it.
Does CMake support Embarcadero? If not, is there maybe some set of projects that I could generate with CMake and then import into Embarcadero.
For a toy example it may work with the bcc and bcc32c, bcc64 is not supported at all.
Maybe the new "free" compiler will help to change that - the press release seems to imply this.
Personally, I would not put my hopes to high. So far, everything I have seen from the company Embarcadero was just a let down.
You can use CMake's "Borland Makefiles" generator and then use Embarcadero's clang based bcc32c to compile the project. Here's their blog post about it: https://community.embarcadero.com/blogs/entry/using-cmake-with-the-free-embarcadero-c-compiler
I didn't find a way to generate project files for their IDE yet, but I'm wondering if one could use one of the "Visual Studio" generators, then manually change the file extension (considering Embarcadero is using MSBuild). But that's just a wild guess.
According to the current CMake documentation on generators, no CMake does not produce Embarcadero project files. Instead, you could use one of the Makefile generators, and set your IDE to run cmake and make when you execute its "build" command.

Using biicode and clion?

Is there an easy way to use clion (e.g. debugging) in a c++ project using biicode as construction tool?
In fact, both systems work with cmake, but biicode generates CMakeLists.txt that clion doesn't seem to understand (the one located in blocks/ nor the temporary one in cmake/).
Right now I could only work by using biicode self-generated CMakeLists.txt for regular builds, and a hand-crafted CMakeLists.txt to compile within clion. However duplicating the description of the construction does not sound like a good idea.
I guess some elaborated dark scripting could be done (I am pretty new to cmake), but I'm just playing around and I don't think it is worthwhile to do it or ask for it.
Has anyone tried to use clion and biicode? Is clion fully supporting cmake yet? Is biicode using internal code that fully cmake-compliant tools won't understand? Am I missing some silly idea?
Currently it is not possible. Unfortunately both biicode and CLion use cmake and use different conventions about the project layout/structure, and at the moment they are simply incompatible.
The good news are that the people at CLion are helping a lot to figure out the best solution so hopefully this will be fixed soon.
EDIT 19-Feb-2015: Now biicode 2.4.1 and last CLion EAP are compatible. You can open an existing biicode project in CLion using these steps:
CLion->Open project, navigate to your biicode project/cmake/ folder and open it (where the CMakeLists.txt lives)
CLion->Change project root, select your main biicode project folder.
Then you should be able to build and run your targets.
It can be convenient to check in Settings->Build, Execution, Deployment->CMake, "Automatically reload CMake on editing".
And remember, if you change your project, add or remove files, main executables, add or remove dependencies, to run $ bii cpp:configure so the whole project is updated
Now biicode and CLion work fully with each other. Here's a guide from biicode docs to use CLion.
Biicode has been replaced by Conan.io, which is far easier to use with CLion.

Making a cross platform library with CMake?

Is CMake difficult to use? I've been developing a library using Windows and MSVC++ . I tested my code in Linux and OSX by tediously making objects from each file and making a library out of this.
I have several Directories ex:
Agui
-> Widgets
->Button
->ListBox
-> Backends
-> Allegro
And what I'd like is, for example, if I'm on Windows, it auto generates an msvc project with all these directories and files included and ready to be compiled.
On Linux and OSX I'd like to be able to do something like
cmake
make
is this possible with CMake, and easy to do given my situation.
Thanks
Yes, that is exactly the reason CMake is made for. Huge projects like KDE use CMake.
And its easy to setup.
This is exactly what CMake was developed to do. CMake generates native build files and workspaces that can be used on your platform of choice. So on Unix this is normally Unix Makefiles, on Windows Visual Studio you can select your Visual Studio version to generate a solution that can be opened in the IDE, or use NMake Makefiles.
If you want to add a simple library, you just use the add_library function, giving it a library name and a list of source files. I would recommend taking a look at the CMake tutorial for a brief overview. There is also a book, Mastering CMake, along with many tutorial pages. You can ask the CMake command line for help too, 'cmake --help-command add_library' would show you the add_library documentation.
CMake will generate project files for a number of IDEs, but I find that automake+libtool tend to be easier to use. Here is an excellent tutorial: http://www.lrde.epita.fr/~adl/autotools.html
Don't be put off by the page count: most of it is step-by-step revealing of diagrams.