Using cmake on windows for c++ - 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.

Related

Compiling and Linking to Visual Studio 2022 using OpenCV source code built as Win32 from CMake C++

I'm trying to use OpenCV with Dear ImGui in Visual Studio 2022. I'm new to C/C++ libraries and building in general, so I'm unsure if I'm doing anything right. ImGui uses 32-bit architecture and I've used Cmake gui to compile the source code as Win32. I think I have the compiled source code, but it seems to be different than downloading the pre-built libraries. File Explorer Screenshot. I've added the bin to PATH environmental variable, and in Visual Studio tried adding \include to Include Directories, \lib or \lib\Debug to Library Directories, and opencv_world460d.lib to Additional Dependencies. The program still runs, but it doesn't seem to include anything related to OpenCV in the #include files. I found a few .dll files in bin\Debug, but I'm not sure if I should bother with that. I think I could move the source code into the project, but I'm fairly certain that isn't the proper way to do it. Any help would be appreciated.
I needed to run the install target:
You may have built the project, but probably you didn't run the install target. Try running cmake --build <build_dir> --config Release and then cmake --install <build_dir> --config Release, where <build_dir> is a placeholder for the path to the build dir shown in the screenshot. The latter command probably requires admin privileges. Probably best to check the docs of the lib, if there's a step by step instruction for building & installing the whole thing. –
fabian

Maintaining Makefiles and CMakeLists.txt

When you're working to very big projects with a large amount of people, maintaining both Makefiles and CMakeLists.txt could be very difficult. I know that CMakeLists.txt could generate Makefiles, but suppose that I want to compile my code by using make and in the same time, use an IDE to have a kind of intellisense. How can I achieve it?
In the scenario that I'm describing, CMakeLists.txt files will disappear, so I won't have the chance to generate solutions with them.
CMake is not a build manager!
It is a generator of files for other build managers!
So you can generate project for IDEs (Visual Studio, Xcode, CodeBlocks, ..) or generate MakeFile, or whatever you prefer and CMake supports. So I do not understand why do you maintain CMakeLists.txt and Makefiles at the same time.
Just maintain CMakeLists.txt and then generate respective Makefiles from it.
For details see CMake Documentation - generators:
Command-Line Build Tool Generators
These generators support command-line build tools. In order to use them, one must launch CMake from a command-line prompt whose environment is already configured for the chosen compiler and build tool.
Borland Makefiles
MSYS Makefiles
MinGW Makefiles
NMake Makefiles
NMake Makefiles JOM
Ninja
Unix Makefiles
Watcom WMake
Bottom line you should be able to generate files for IDE or Makefile for you favorite build manager without any complications.
In the scenario that I'm describing, CMakeLists.txt files will disappear, so I won't have the chance to generate solutions with them.
WAT? You are doing something very strange and most probably wrong and you didn't provide any details abut that.
Concerning intellisense I would say:
Visual Studio 2017 as built-in support for CMake, see announcement
CMake can generate a json file with all commands see CMAKE_EXPORT_COMPILE_COMMANDS
so you can use it to have smart clang based completion in vim
QtCreator as built-in support for CMake so you'll have completion too.
Extra tip at first: Do not try to mix both handmade make files and CMake over the whole time of the project.
The normal way is to create the make files using cmake. The extra of cmake is the cross platform and in your case the nativ support by some IDEs, eg. QtCreator, CLion(awaik). For a wide range of other IDE cmake can generate the project files.
Example Workflow using QtCreator:
Create a simple CMakeLists.txt, at least with the name of the project
Load this into QtCreator
Add source files, update the CMakeLists.txt from within QtCreator
Build from within QtCreator, repeat
But the same CMakeLists.txt will be used to create the make file on command line. Or on your build server or CI system.

How can I build curlpp on Windows for gcc?

I've downloaded the package from here: https://github.com/jpbarrette/curlpp
When I drag CMakelists.txt onto cmake.exe it does build something that looks like a Microsoft Visual Studio project. I want to build static library that I could use in Codeblocks with GCC instead. I've not idea how to do it, I don't know where the options are or how to set appropriate flags.
I've also followed this post: https://stackoverflow.com/a/27609214/7310666c which is about cURL - it build correctly, but when I linked it to my project it gave me undefined reference error.
Could anyone give me a hand here? My desktop is already littered by all the files I downloaded while trying.
As mentionned here:
CMake [..] is responsible for writing the input files for a native
build system.
The modules responsible of creating files for a specific build system are called "Generators". For a full list of those generators, please have a look at this.
In your case, by default it selects Visual Studio, even though "Code Blocks" should be available.
So, here's an example of how you could obtain the required files:
cd /path/to/curlpp/
mkdir build
cd build
cmake -G "CodeBlocks - NMake Makefiles" ..

Changing current cmake generator

How can I change current cmake generator without using Cmake GUI (I want to change standart generator to Visual Studio 12 2013 Win64)?
For those seeking the CMake GUI answer.
Using the GUI menu
Go to File->Delete Cache and then click Configure again.
A scenario where changing the generator is needed is that you are keeping the CMake GUI open and reusing the same directory (source and CMakeList.txt) to do simple examples or tutorials and deleting the build files before starting again, e.g. with out-of-source build it would be the entire build directory. This is not the standard use case for CMake but one that would be common among beginners.
You cannot reliably change the generator used for an output (= binary) directory once generation has already happened there once. You should start in a fresh output directory.
Once there, simply use CMake's -G command-line option:
cmake -G "Visual Studio 12 2013 Win64"

how to setup wxwidgets on windows for visual studio 2005

i am try to do some GUI stuff and i want to setup wxWidget for visual studio 2005 on windows 7. I read may tutorial and help in last few days but all of them are showing different ways. and none of them work for me. in include c:/wxWidgets-2.9.4/include and /lib directories to visual studio but still it showing wx/setup.h not found.
can any one please give a tutorial for setup wxWidget from scratch.(from downloading required packages to full working example). because i am also confuse which one i should download, they provide many zip forders.
#tinman's recipe allows you to build eveything, but it takes a long time.
A simpler recipe, which only builds the necessary library:
Do the installation using the setup package.
Find the microsoft visual studio build folder. On my installation this is C:\wxWidgets-2.9.n\build\msw
Find the solution for your version of visual studio ( e.g. wx_vc8.sln ) and open it.
Select the configuration you intend to use when building your applications. ( e.g. DLL Release )
Build the solution.
Done. It should take about five minutes on any reasonably powerful machine.
The reason there are many zip archives is that from 2.9 onwards they are providing pre-built binary packages for each MSVC version. I have never used these so I cannot comment on those, but they might save you a lot of time building the sources and give you an official library.
I have used the MSW setup package and built from source. This is what I do and it works well for me (except I currently use wx292, VS2008 and Windows 7).
Download the wxWidgets 2.9.4 MSW setup package.
Install it. I assume it will create a directory in c:\wxWidgets-2.9.4.
Create a batch file c:\wxwidgets-2.9.4\build.bat with the following contents. You should replace myvendor with your name so it is clear that it is not an official wxWidgets library build (it will be put in the filenames, so avoid spaces).
#echo off
PUSHD build\msw
call :make
cd ..\..\samples
call :make
cd ..\demos
call :make
cd ..\utils
call :make
POPD
GOTO :EOF
#REM Perform builds with all desired configurations
:make
#REM x86 debug & release multiple DLLs Unicode
nmake -f makefile.vc MONOLITHIC=0 SHARED=1 UNICODE=1 BUILD=debug VENDOR=myvendor
nmake -f makefile.vc MONOLITHIC=0 SHARED=1 UNICODE=1 BUILD=release VENDOR=myvendor
#REM x86 debug & release multiple static libs Unicode
nmake -f makefile.vc MONOLITHIC=0 SHARED=0 UNICODE=1 BUILD=debug VENDOR=myvendor
nmake -f makefile.vc MONOLITHIC=0 SHARED=0 UNICODE=1 BUILD=release VENDOR=myvendor
Open a Visual Studio 2005 command prompt from the Start Menu. This will set up the path to your compiler and libraries correctly.
Change directory to wxWidgets: cd c:\wxWidgets-2.9.4.
Run the build.bat
It should complete after a few hours depending on your computer specifications, or you can REM out the builds for the samples, utils and demos to reduce the time (thanks #ravenspoint). You can also see the examples in c:\wxWidgets-2.9.4\samples and get the Visual Studio project settings from the sample projects to use in your own projects (for example C:\wxWidgets-2.9.4\samples\dialogs\dialogs_vc8.vcproj).
After building the library you will end up with some additional directories under C:\wxWidgets-2.9.4\lib which contains the built libraries, dlls and any build specific headers for that variant. The following directories are created from the builds in my batch script and are tagged with the toolset (vc for Visual C in this case, but could be version specific, e.g. vc80 or a fixed version depending on your build settings) and the type of library. msw in the directory and filenames means MicroSoft Windows. u in the directory and filenames means Unicode. d at the end of directory and filenames means Debug.
C:\wxWidgets-2.9.4\lib\vc_lib: Visual C (vc) Static library (lib) versions of wxWidgets
C:\wxWidgets-2.9.4\lib\vc_lib\mwsu: MicroSoft Windows Unicode specific files
C:\wxWidgets-2.9.4\lib\vc_lib\mwsud: MicroSoft Windows Unicode Debug specific files
Also contains a mixture of static libraries for release and debug
C:\wxWidgets-2.9.4\lib\vc_dll: Visual C (vc) DLL (dll) versions of wxWidgets
C:\wxWidgets-2.9.4\dll\vc_lib\mwsu: MicroSoft Windows Unicode specific files
C:\wxWidgets-2.9.4\dll\vc_lib\mwsud: MicroSoft Windows Unicode Debug specific files
Also contains a mixture of DLLs and import libraries for release and debug
In order to use the newly built libraries in your own projects you can follow these steps (I use VS2008 so some of the configuration names might be slightly different):
When you create a new project you need to go to the project properties.
Under Configuration properties -> General -> Environment add the following (so your application can find the DLLs) if you are using the DLLs:
PATH=C:\wxWidgets-2.9.4\lib\vc_dll
Under C/C++ -> General -> Additional Include Directories you need to add the wxWidgets includes in the following order:
"C:\wxWidgets-2.9.4\include\msvc"
"C:\wxWidgets-2.9.4\include"
The reason for including directory 1 first is that there is a wx\setup.h in that directory. When you include that it tries to determine what compiler, type of library (static or DLL), whether you are using Unicode or not and whether it is debug or release and it automatically includes the correct real wx/setup.h from the one of the subdirectories of C:\wxWidgets-2.9.4\lib.
You then need to go to C/C++ -> Preprocessor -> Preprocessor Definitions and add the following lines:
__WXMSW__
If you want to use the DLL version of wxWidgets instead of the static library you need to add the following line:
WXUSINGDLL
Under Linker -> General -> Additional Linker Directories you need to add the following path if you are using the static libraries:
"C:\wxWidgets-2.9.4\lib\vc_lib"
Or the following path if you are using the DLLs:
"C:\wxWidgets-2.9.4\lib\vc_dll"
In Linker -> Input -> Additional Dependancies you will probably need to add at least the following:
comctl32.lib rpcrt4.lib
And under Resources -> General -> Additional Include Directories you will need to add the following path:
C:\wxWidgets-2.9.4\include