Inferring include paths and make targets from existing Makefile in Eclipse - c++

I am trying to use Eclipse on an existing collection of folders with C++ and recursive Makefile files in Linux. The make files use gcc and ar, and the user specifies the path to the gcc he wants to use in the Makefile. The make files that I will be working with were typed by hand.
In Eclipse, there is an option to create a new project that looks appropriate for what I need: "Create a new Makefile project from existing code in that same directory".
* <none>
* cross GCC
* GNU Autotools Toolchain
* Linux GCC
Here I have two questions:
Which toolchain should I choose? and how does Eclipse use this information?
I would like Eclipse to infer as much as it can from the top-level Makefile (e.g. include paths, make targets, etc) . How can I do this?

Your question is very similar to what I have setup for my project.
If the target platform that the application is expected to run on is the machine you are compiling on, the you ought to select Linux GCC.
If the target platform that application will run on is eg an ARM processor, you need to specify cross GCC.
The GNU Autotools Toolchain is designed to build source-code packages on different linux systems.
By selecting "Create a new Makefile project from existing code in that same directory" eclipse will assume that the user's makefiles will manage the build itself.
For eclipse to infer include paths, have the discovery options enabled in the project settings. Admittedly, your top-level make needs to specify include path, source-code paths etc.

You really can only eiter allow Eclipse to create the makefile for you based on what you have in your project or tell it which command to run when you select 'build'. Mixing these options I have found doesn't really work. If you are specifying the Makefile then the compiler used doesn't really matter either since the Makefile will define that. Just pick an 'empty c++ project' when you create a new project, make sure you specify not to use the 'default workspace' location and point this input box to your code base instead. In the project c++/build settings make sure that the "automatically generate makefile" is unchecked and specify what command should be executed to make your project. (usually this is 'make all' but it might be different for you)

Related

How to generate a C++ executable file on Clion (using Ubuntu)

I'm currently learning C++ and I'm working on a small project. I was wondering how I can generate an executable of my project. I'm not sure how to do it, I'm using Ubuntu 20.04
I've googled it but I can not find any instructions on how to do it.
Well. If you are using CLion, you can just create a new project, and check the CMake file that CLion generate. Something like this:
cmake_minimum_required(VERSION 3.20)
project(Test)
set(CMAKE_CXX_STANDARD 14)
add_executable(Test main.cpp)
And in that file there was a line with a function called add_executable, in that function you set first the exe name and then the source files. And just run the project in CLion. By default CLion create a directory calle "cmake-build-debug" where the exe file are located.
If you want to add more libraries, change the binaries source directory and more. You will need learn CMake. Also you can use CMake standalone whiteout CLion, you just need install it sudo apt-get install cmake and use then in the terminal.
Expanding over #Steback's answer:
First, a clarification: an executable file is a file that can be executed by the system. It (roughly) contains assembly commands. Under Windows executable files are marked with an .exe extension. Under linux, they are usually extension-less.
To generate an executable file from C / C++ code you ("only") need a C/C++ compiler. A default / pre-installed one on Ubuntu is gcc / g++ (whereas on Windows you need to actively install one).
CLion is an IDE and (exactly like any other IDE) can run the compiler for you. IDE (stands for Integrated Development Environment) is a program which incorporates (minimally) a text/code editor, a compiler and a debugger (all of which it invokes normally via command line, just as you can do yourself).
CLion is an advanced (and excellent) IDE. In CLion, the way you specify how exactly it should invoke the compiler is via the CMake language (not to be confused with the unix tool make which chiefly only knows to run commands conditionally on file modified date).
CMake code should be placed in a file named CMakeLists.txt in the project root directory (sometimes CLion creates this file for you automatically). A minimal cmake project looks like
# Specify cmake language version to use for this file
cmake_minimum_required(VERSION 3.10)
# Specify any name for the project
project(NameYourProject)
# A name for your executable file and the code files needed to build it
add_executable(YourExecutableName source_file1.cpp somefolder/source_file2.cpp header_file.h)
Of course this is just a very minimal example. The CMake language is a powerful language to specify build processes, with cross-platform support. You can look it up / learn it someday.
When giving the "build" command to CLion it will now do two things:
use the cmake tool to generate a set of commands for gcc/g++ (this is called "cmake configure" + "cmake generate") - according to what you wrote in CMakeLists.txt
run the generated commands to hopefully build your executable.
As a beginner, you may be better off first trying to run the compiler yourself via the command line to see what it does. You can also opt for a different IDE (e.g. CodeBlocks, eclipse, Dev C++) where you specify what you need the compiler to do via a GUI and not via CMake (although CMake is arguably more convenient).
I'm using Dev C++. When I run and compile it, it automaticcally generate a .exe file. Just install it and open ur file with it and compile it. Should work good.

Export Eclipse Compiler to cmd

I try to compile an eclipse-project written in c++ which is building a .so-File. Now i try to build the project without an IDE. I tried it with MinGW but the created .so doesn't work. I think there are used some special packages but how can I get the information's.
Is there a way to Export the build settings and import them into MinGW?
How can i else set the compiler?
This is the ToolChainEditor which i would recreat in MinGW.
You can't import a configuration into mingw, but you can tell mingw where to find a configuration of sorts.
Eclipse can be configured to generate a makefile (At least as recent as Neon). Select your project and use the main menu to navigate Project->Properties->C/C++ Build->Tool Chain Editor. Set the "Current builder" to Gnu Make Builder and build the project.
In the generated folder where you would find the finished executable you will find three more files: makefile, objects.mk and sources.mk and can use the make utility that often comes with mingw. It can also be downloaded separately if you have to.
From the command line, enter the folder and type make. Magic will happen. If you want to know more about the magic, make is a very deep topic well worth learning, even if only as a gateway drug to more modern build systems that descended from it.

Using scons to compile C++ code under windows, scons adds "/Fo" as compile option

Using the following:
Python version 2.7.13, Scons version 2.5.1, Visual Studio 2012 express is installed, but I am not planning to use it.
Code blocks and MinGW-W64-builds-4.3 are installed.
Using Scons to compile C++ code (networkit toolkit) under windows. Scons adds "/Fo" as compile option. This option works only with VC++ and not with MinGW which I am trying to use. Why does Scons add this flag? I have checked my Sconstruct and the reference build.conf files and cannot seem to find this flag getting set explicitly.
My Sconstruct file is here(http://www103.zippyshare.com/v/jSrMapGz/file.html) and the build.conf file is here (http://www11.zippyshare.com/v/aXGQA5b5/file.html).
I want to get the compilation done with "-o" flag for g++, which is the equivalent of /Fo flag for VC++. I just cant figure out where Scons is picking this flag from :(
I am a novice with python and scons. I typically use VC++ 2012 but have to use networkit toolkit for a project, but it uses C11 features. And I cannot update to VC++ 2015/2017 yet.
Thanks for your help!
I checked your SConstruct file, and you are initialising your build environment as
env = Environment()
, which leaves the environment variable "tools" set to its standard value "default". The latter setting means: let SCons figure out which tools/compilers are installed in the current system, and add corresponding Builders to the build environment automatically. Under Windows, SCons will prefer "vc" over "mingw"...this is hardcoded at the moment (we're working on changing this for future versions of the core source).
What you can do, since you know that you have a "mingw" compiler installed that you want to use explicitly, is to tell SCons that you want to work with "mingw" only.
The following example from the page https://bitbucket.org/scons/scons/wiki/SconstructShortMingwWin32 shows the basic recipe for this:
import os
#don't use the default environment
DefaultEnvironment(tools=[])
#create an environment that uses mingw tools
env = Environment(ENV=os.environ, tools=['mingw'])
#the target will be myprogram.exe (in win32)
#the source files will be every file in the
#current directory that matches "*.cpp"
env.Program(target='myprogram', source = Glob('*.cpp'))
For further help and as reference, please consider checking out our User Guide and Man page.

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.

How to package C++ with dlls and libraries

I'm wondering how to "package" a C++ project for release. It uses various libraries, and I don't want a user to have to go through the same setup I did, with putting the right files in the right place and such. I had difficulty researching this, because I'm not sure the technical term for this issue. If I'm using command line compiling on Linux, is there an easy way to do this?
Your approach to this will differ on Windows and Linux because each OS handles this a different way. I'm more familiar with Linux so I'll restrict my answer to just the Linux side of things.
When you link your executable with a library using -l flag the linker defaults to looking in the normal system library directories so there are four approaches here.
Require the user to properly install the libraries themselves. However, it sounds like you don't want to do that.
Have the user add the library location to LD_LIBRARY_PATH variable.
Your third option is force the linker to look in a certain path for the libraries using the -rpath flag. For example, to have the application look in its working directory for a shared library you can compile with: g++ -rpath ./ -l SomeLib -o MyApp myapp.cpp
One other option is to static link your code with their library that way you only have to distribute one executable. If a static library exists you can use g++ -static -l SomeLib -o MyApp myapp.cpp to tell gcc to link statically.
On windows I would recommand wix http://wix.sourceforge.net/ to create the .msi installer
I would like to point out, the lookup path for .dlls I recommand putting all .dll in the same folder as your .exe since this has the highest priority
However, the vc crt (the c/c++ runtime library) should be installed using the redistributional package from microsoft -> updates automatically http://www.microsoft.com/de-de/download/details.aspx?id=5555
Wix can include the redistributional package into the same .msi therefore you have only to deploy a single installer file.
You mean an installer?
On Windows the program that you run to install a new app which outs everything in the correct directory, creates the start menu and lets you un-install it?
There is an installer builder in Visual Studio (might not be in the free express version) which makes .msi installer files. It's fairly easy to use for simple tasks but becomes complicated to do anything more.
Alternatively, to create traditional setup.exe type installs I use the excellent free Innosetup
On linux you would generally create a package using whatever format your distribution uses (.deb / .rpm ). There are lots of instructions on the specifics of each one and the tools to do so will probably already be installed in your Linux system