How to build projects in parallel using Eclipse/CDT (not parallel compiling!) - c++

I have several projects which all rely on a basic library. Now when I change a header file in this basic library I have to rebuild all dependent projects. Currently Eclipse/CDT builds one project after another. How can I build all these projects in parallel?
Please note that I already use the -j (parallel compiling) option for each project. But this is not enough because:
there are really a lot of CPU's available (for most projects more than source files) and
due to parallel compiling the linking takes much longer than compiling and (to my knowledge) can not use multi-threading.

I don't think the current version of Eclipse CDT can build projects in parallel, but you can achieve the same effect by having an Eclipse CDT Makefile project which builds multiple binaries (libraries and executables).
A simple way to generate such project is by using CMake with Eclipse CDT4 - Unix Makefiles generator. Then if you specify /usr/bin/make -j<n> as a build command, where n is the number of parallel jobs in Project Properties, it will build your targets (not only source files) in parallel.

Project Properties > C++ Build > Behaviour > Enable Parallel Build

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.

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.

Faster way to link libraries in Eclipse CDT

I'm using Eclipse CDT Neon Release 4.6.2 on Ubuntu 16.04. I work with project where I need to include and link OpenCV 3.1 libraries. For each individual project, I always go to Window > Preferences > C/C++ Build > Settings and add the paths and names of the relevant OpenCV libraries under "Cross G++ Compiler > Includes" and "Cross G++ Linker > Libraries".
Is there a way to avoid doing this for every new project, e.g. through a configuration file for the required libraries which I can simply copy for every project? Entering the same information each time, especially the names of the libraries, is very time consuming.
You could use Makefile projects rather than Managed Build projects (in the New C++ Project dialog, select "Makefile project" as the project type instead of "Executable"/"Shared Library"/etc.).
You would then have to write a makefile that specifies what compiler commands to run to build the project, including the flags for OpenCV's include path and library path.
Once you write the makefile once, you can reuse most of it for different projects, either by copy-and-paste, or by factoring out the reusable bits into a shared makefile that you include from the projects' makefiles.
I switched to using Makefile projects exactly for this reason.

Is it possible to force CMake generate Visual Studio project on other project type generation?

I have a CMake project. It is a crossplatform project developed by a team of developers. Visual Studio and other make files are inside version control for library release and external developers.
Each time a file is added we need to recompile all project files for all platforms. How do I force CMake to generate new project files for all systems at once (if possible from inside CMakeLists.txt, not as command line arguments)?
I think it doesn't make sense for this to be possible within the CMakeLists.txt file. CMake is a makefile generator. Everything in the CMakeLists.txt file is configuring the makefile, and it can also be repurposed to make project files.
If the CMakeLists.txt file could also request to generate a different kind of makefile... it would be different from every other command in the CMakeLists.txt file in that it isn't describing the currently selected makefile.
If I were you I would just make a shell script, or a simple makefile, separate from CMake, which rebuilds each of the project files, by invoking CMake from command line with appropriate parameters.
Is the goal of the versioned CMake produced build scripts to not force developers to install CMake?
In any case: it's best to use the right tool for the right job. CMake is for producing build-files and the little scripting necessary to do so. Use a scripting environment (Bash, cmd.exe) to run CMake as necessary for all your platforms.
This keeps the CMake files clean (and readable, CMake scripting is hard to read) and provides clean separation of concerns.

Prevent Eclipse CDT rebuilding dependencies when there are no changes

I have been using Eclipse for the past two years for an embedded linux project.
I am able to run my code on Unbutu host unchanged and as such do most of my testing
on the host. Each project has a Debug configuration for host and Target-Debug for
embedded linux device that builds with cross compile toolchain.
My workspace has more than 20 projects which consist of libraries and executables that
reference the libraries. The problem I have is that anytime I build an executable there seems to no rhyme or reason to the way eclipse rebuilds the referenced libraries. It will nearly always build several of them when no changes to any of the code in the libraries has changed. When it builds the libraries it builds all of the source in them, not just a few files. In some cases it will do so even when the target executable is up to date and not itself rebuilt. I can clean the executable, then build. Right away do another build and instead of reporting everything is up to date it might still build some of the libraries. Build another time and it might build the same libraries again or in some cases finally report that everything is up to date.
Every build configuration for an executable has the exact library build referenced as opposed to "active". For example for a Target-Debug executable build configuration I reference the Target-Debug build of the libraries it depends on. When I build executables it is often possible that the "active" build configuration selected for libraries are not the same as executable I'm building but that shouldn't matter if the build configuration for the executable references specific library build configurations.
At one time the problem included rebuilding configurations that weren't even of the same
type. That is, building a Debug version of executable would also cause Target-Debug configurations of libraries to be rebuild also. That problem seem to be resolved by unchecking Project References (as opposed to C/C++ General/Paths and Symbols/References).
I experienced this same problem with Helios, Juno and now Kepler with respective CDT releases.
How can I prevent eclipse from re-building dependent library projects when they haven't changed? I have tried everything to figure out why it's happening and search for solutions. Maybe some misunderstanding on my part of some concept in eclipse build? I'm at a loss...
Any advice would be greatly appreciated.