Project Linking and Compiling files - c++

I want to start building a project and I have the following folder structure:
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
I have the MinGW compiler and I don't know how to compile all .cpp files. I know the command g++ *.cpp -o main for compiling all the files, but works only for files in the same folder.
Should I move all my files to the src folder? Should I change the project structure?
Also, I'm really doubtful if I should use CMake or not.
FINAL:
I decided to go with CMake which made my life easier.

For a barebones project, your structure is fine. Just add the following CMakeLists.txt file to the root of your directory:
cmake_minimum_required(VERSION 3.5)
# Given your project a descriptive name
project(cool_project)
# CHoose whatever standard you want here... 11, 14, 17, ...
set(CMAKE_CXX_STANDARD 14)
# The first entry is the name of the target (a.k.a. the executable that will be built)
# every entry after that should be the path to the cpp files that need to be built
add_executable(cool_exe src/main.cpp lib/class1.cpp)
# Tell the compiler where the header files are
target_link_libraries(cool_exe PRIVATE lib)
Your directory should now look like
CMakeLists.txt
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
Then to build the project, you will typically
Make a folder where you build everything (often called build, but it's up to you). Now the directory looks like
CMakeLists.txt
lib
|---class1.cpp
|---class1.hpp
src
|---main.cpp
build
Go into the build folder and on the command like, configure your project with the command cmake .. (just to reiterate... this needs to be done from inside the build folder).
Build your project with the make command (again from inside the build folder).
After that, you should have an executable called cool_exe in the build folder.

Related

How to display files generated to build space in QtCreator

I use a CMakeLists.txt like the following
cmake_minimum_required(VERSION 3.5)
project(generate_files_from_tool)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_custom_command(
OUTPUT generated_config.cpp
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/writeSourceFileFromConfigXml.sh ${CMAKE_CURRENT_SOURCE_DIR}/config.xml > ${CMAKE_CURRENT_BINARY_DIR}/generated_config.cpp
DEPENDS config.xml writeSourceFileFromConfigXml.sh
)
add_executable(tool_with_generated_file main.cpp generated_config.cpp)
which generates a file (here generated-config.cpp) in the build process using a custom script (writeSourceFileFromConfigXml.sh).
I can open it using QtCreator 4.4.1 just fine: Compile and run works -- but I cannot see the file generated_config.cpp in the Projects view, only CMakeLists.txt and main.cpp is visible there.
If I open the file "manually", QtCreator displays a warning "This file is not part of any project." -- but it uses the file for the compilation. And after opening the file, I'm able to switch between definition and implementation using Ctrl+Klick or F2 (and get to/come from generated_config.cpp)
So my question is: How can I make QtCreator understand, that this generated file is part of the project and add it to the Projects view?
Note: changing the last line to add_executable(tool_with_generated_file main.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated_config.cpp) did not change anything.
I am using a workaround to show generated sources located outside of build directory:
I have symlinks created in PROJECT_SOURCE_DIR to directories where generated sources are located, say src1, src2 (it is important to not use real paths). Then I use :
file(GLOB SOURCE_FILES ${PROJECT_SOURCE_DIR}/src1/*.[ch] ${PROJECT_SOURCE_DIR}/src2/*.[ch])
add_custom_target(
all_src
SOURCES ${SOURCE_FILES}
)

Building a cmake project with multiple directories

I have been given some C++ which I want to use in my project. This code comes with its own CMakeLists.txt file. To keep things neat, I want to make a new directory foo in my main project directory, and put this code and the CMakeLists.txt file in that directory. What I now want to know, is how do I edit the CMakeLists.txt file of my own project, to include all the source files from this other project?
Normally, I use file(glob SOURCES *.cpp) to create a list of all source files. Then, I would create the executable by using add_executable(${PROJECT_NAME} ${SOURCES}). However, how do I tell SOURCES to also include all the source files in the foo directory?
Now, if I use add_subdirectory(foo) in my main CMakeLists.txt file, then I believe this will search directory foo for a CMakeLists.txt file, and effectively add this to the main CMakeLists.txt file in my project. Is this correct? How can I extend this to including all the source files in this directory?
Thanks!

CMake file for a C++ project

There is some CMake magic I don't understand. How should a CMakeLists.txt file look like for a small C++ project with directories like this:
.
├── bin
└── src
├── src
└── test
bin — directory for built program
src/src — directory for source
src/test — directory for tests
The tests will need to include files from src/src.
I'd like to manage all the operations from cmake, however at this moment I even can't cause cmake to compile file in src/c.cpp.
Any help, links are welcome.
Your CMake files should reside in the main source directory and its sub-directories. The easiest approach is to have one CMakeLists.txt in the src directory, which includes all files from src/src and src/test. A very minimalistic example could look like the following:
# CMakeLists.txt in src
project(myExample)
set(myExample_SOURCES
src/file1.cpp
src/main.cpp)
add_executable(myExecutable ${myExample_SOURCES})
set(myExample_test_SOURCES
src/file1.cpp
test/test_file2.cpp
test/test_main.cpp)
add_executable(myTestSuite ${myExample_test_SOURCES})
The output directory is normally not specified, because you can have different active
builds in parallel with
different options, e.g. you can have one build in debug mode -O0 -g, another one in release mode with -O2 -g flags and a third one in release mode with heavy optimization flags -O3. Every build resides in its own directory (e.g. build-debug, build-rel,
build-opt).
You should create the output directory (bin in your case) manually and call the cmake command inside this directory. As an argument you have to supply the path to the main CMakeLists.txt. In other words, just execute
cmake ../src
when you are inside bin. This will take all files from the src directory and put the output to the bin directory.
You can easily create a second output directory, say bin2, where you specify different build flags. The ccmake provides a very minimalistic GUI for that.
This helped me to start with cmake examples.html

CMake and Qt : directory for moc files / clean rules

I find CMake a little difficult to understand, and I currently have a problem with directories.
For the example, let's suppose that I have the following directory organisation :
project/
project/bin
project/obj
project/src
project/src/main.cpp
project/src/mainwindow.cpp
project/src/mainwindow.h
project/CMakeLists.txt
The project uses Qt 4.8 and CMake 2.8.8.
I want to do the following things :
At the beginning of all compilation, all previous temporary files are cleaned (.o and .moc)
All the .o and .moc are put in the obj directory
The executable is put in bin
What is the simplest CMakeLists.txt to do that ?
Thank you very much.
It is recommended to use out of source build where all the generated files are outside of the source folder. Is there any reason you are not doing that approach?

How to compile multiple files in ROOT

I wrote a C++ program (with a main.cpp, and various header and implementation files) that compiles fine under g++. Now I am trying to compile it in Cern's ROOT library, but I do not know how to, since the only way I know how to compile files in ROOT is by using .L main.cpp.
How can I include multiple files in ROOT?
The most reliable way to use ROOT (at least historically and currently) is to ignore the interpreter other than for the simplest explorations and explicitly compile your C++ programs against the ROOT libraries. For example, use
g++ MySource.cc `root-config --libs --cflags` -o foo
to compile an executable "foo" from a single source file. For more info on that helper script run "root-config --help".
Multi-file programs/libraries are nothing special provided that you supply the required args to point at the ROOT libraries and headers (and that the libs are available in LD_LIBRARY_PATH at runtime.) Standard C++ guides will explain that step if needed. You can safely put this into a makefile, too.
For my money this is both easier and more reliable than using the .L etc. commands in the CINT interpreter. Last time I tried, ACLiC was actually compiling against a temporary (and mangled) version of the specified source file, so any error messages from the compiler were pretty much useless!
I use CMake to compile my ROOT-based projects.
If you have a project directory proj/ and it contains src/ and bin/, you'll need 3 CMakeList.txt files, one in each directory.
A simple example CMakeList.txt in the main project directory:
cmake_minimum_required(VERSION 2.6)
project (SOME_PROJ_NAME)
add_subdirectory(src)
add_subdirectory(bin)
src/ directory is where you keep your .h and .cxx proj. library files. Example CMakeList.txt file:
# get all the *.cxx filenames, to compile them into a lib
file(GLOB SOME_PROJ_LIB_SRCS "${PROJECT_SOURCE_DIR}/src/*.cxx")
# include ROOT library and include files
include_directories(/path/to/root/dir/include/dir)
link_directories(/path/to/root/dir/lib/dir)
# and compile src into a library
add_library(Proj_lib_name ${SOME_PROJ_LIB_SRCS})
# here, list the ROOT libraries you require
target_link_libraries(Proj_lib_name dl Core Cint RIO Net Hist Graf Graf3d Gpad Tree Rint Postscript Matrix Physics MathCore Thread Gui pthread m)
bin/ directory is where you keep your app .cxx files and it has a CMakeList.txt file:
include_directories(${PROJECT_SOURCE_DIR}/src)
link_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(/path/to/root/dir/include/dir)
link_directories(/path/to/root/dir/lib/dir)
add_executable(example_app.exe example_app.cxx)
target_link_libraries(example_app.exe Proj_lib_name dl Core Cint RIO Net Hist Graf Graf3d Gpad Tree Rint Postscript Matrix Physics MathCore Thread Gui pthread m)
Finally, to compile ROOT-based code with CMake, out of source, you create a "build" dir in your top level project dir, so that your dir structure looks like this:
proj/
bin/
build/
src/
Then
cd build
cmake ..
Your binaries will be located in build/bin/ directory
Hope this helps.
It appears that I would simply .L <filename> for each file I want, since .L tells ROOT to "load the contents of file in memory." Though now I am not too sure which order to load the files in, as they are giving me errors.