CMake add_executable() use .cpp file or .hpp file? - c++

I've read a bunch questions on this site about the usage of add_executable function in CMake, but have not found an exact answer to my confusion.
My question is why we only add .cpp files in the add_executable function but not .hpp files?
I understand header files are like "indices" for functions and classes in the corresponding .cpp files. But if we don't include them in the add_executable function, how are they used in the build process?
For example, will the A.hpp file be used when another source file import A.hpp? But then A.hpp is not in the add_executable function... How does the program know where to find A.hpp?
Thanks!

Header files, which often have .h or .hpp extension, although not always - for example, C++ standard library headers have no extensions - are "copy-pasted" by compiler into every .cpp (or .C, or .cc) which has #include directive to include the file.
Because of that, build system, such as CMake, doesn't have to know about them when the final executable is built - their contents is already accounted for by literal inclusion of their code into .cpp file.
However, build systems need to know about those files when dependencies are specified - to ensure that the whole application is rebuilt whenever any of those files is updated, and also to provide the proper inclusion path to the compilation command.

Related

How to include .cpp and .hpp files of a library in my c++ project in visual studio

Apologies for a very basic question. I am trying to port from a CMake project into a visual studio project.
Basically I want to run the project from visual studio without using the CMake file. In the project I need to port, there are many folders and sub folders that contains many .cpp and .h files. These are included to the main cpp file as using #includes.
My Case
The library I want to include
and my main.cpp
For instance to make things simple assume I have a main file main.cpp and this file includes #include "Libpfs/colorspace/colorspace.h". The Libpfs is a folder and it has many sub folders one of which is colorspace folder and this has many .h and .cpp files. One .h file is colorspace.h that is included in main.cpp using the #include and the folder also has .cpp file i.e. colorspace.cpp.
My Attempt
My objective is include them to my project.
Now here is what I have tried
in Visual Studio Project->properties->C/C++ in Additional Include Directories I gave the path of the folder that contains Libpfs but this approach did not work and gave linker errors this might be because I have no lib files for the Libpfs (correct me if I am wrong). I only have .cpp files of the corresponding .h files. I presume the cause of error is the the .cpp files are not compiled yet.
My Question
How can I include the cpp file to my project as well (not the lib files since I dont have those).
Using this for a source, I find the CMakeLists.txt to contain nothing special.
FILE(GLOB COLORSPACE_H *.h)
FILE(GLOB COLORSPACE_HXX *.hxx)
FILE(GLOB COLORSPACE_CPP *.cpp)
SET(LIBPFS_H ${LIBPFS_H} ${COLORSPACE_H} ${COLORSPACE_HXX} PARENT_SCOPE)
SET(LIBPFS_CPP ${LIBPFS_CPP} ${COLORSPACE_CPP} PARENT_SCOPE)
So you can just add all the files to a VS C++ project. I would use some directory management, to separate these sources from your own.
But anyhow, in that case, you should include the sources by relative path. E.g.
#include "../../Libpfs/colorspace/colorspace.h"
Alternatively, you could put everything in a separate C++ library (static .lib or dynamic .dll). In that case you should but the binaries in in a bin path and add that as additional library directory (project properties of your own project) and put all the header files in an include/Libpfs path and add that as additional include directory. In that case you should include the files as.
#include <Libpfs/colorspace/colorspace.h>
On another topic
#define pow_F(a,b) (xexpf(b*xlogf(a)))
I found this define only in the sources of the same source used above.
It seems to be sourced from sleef and according to this it should give a speedup. But you should measure if that is really still true, instead of doing copy-paste/cargo cult programming.
I think generally you should use the standard library std::pow, which has overloads for float, double and long double. The compiler will in most cases optimize its use for you.
open explorer , look for C:\Program Files\Microsoft SDK\ and then go after dir with alot of .lib's in its \lib .
it's an msvc source for .lib and other stuff like that . copy your library there

Build makefile with source and header in separate folder

I'm trying to build a Makefile with separate folder for my sources and my headers. I have a the root of my project that contain an include folder that holds my .hpp files, and a source folder that holds my .cpp files. How can i build the Makefile that it builds all the .cpp with the respective .hpp files ?
How to create the program output in a folder called build ?
Thank you in advance
Make is so diverse, there's a LOT of ways to do this, but the end result is -I<your hpp path> (or -J) needs to be passed to the compiler to tell it additional include paths to search when resolving #include
The path needs to relative to the invocation of the rule directory, or relative to the file (I'm pretty sure the compiler searches both).
A lot of makefiles use CPPOPTS and CCOPTS or some variant of that in the makefile to pass extra options to the C or CPP compiler. Try adding:
CPPOPTS += -I..\include
To your makefile (assuming you've segregated your source and include files that way).
Again, this is ALL dependent on your makefile.
I'm trying to build a Makefile with separate folder for my sources and my headers.
An alternative way would be to have your headers in the same directory as your .cpp files. And in that top level include folder you can put symbolic links to the headers.

Definitive way to include files on c++ avoiding cyclic dependencies

I always have problems with c++ on this, I spend more time trying to solve dependencies instead of programming when I setup a new project. I search the internet a way to do this automatic, or softwares that do that. In fact, I always program on geany and compile with shell script files...
So, is there a software to manage this? Do IDE's do that?
I always include .cpp files on my main.cpp and then I include the .hpp files on these .cpp. So, if I have a main.cpp, a object.hpp and a object.cpp, I will include the object.cpp in the main.cpp and the object.hpp on the object.cpp. Is there a better way to do that?
Can I just include the .hpp files and in the build script add every .cpp file?
I just cant find the answer on the internet, maybe im doing the wrong question...
I have found a nice article dealing with including files.
Common practice for all c++ header files is to simply define inclusion guards.
#ifndef TEST_H
#define TEST_H
// class definitions goes here
#endif
If there are some cyclic dependencies, consider forward declaration.
Every-time this header is included, the compiler checks, whether symbol TEST_H has been defined already. This basically guarantees, that contents of this file are included only once, and so that there is single declaration of the classes, defined in header file.
Good to know is, that directive "#include <>" does copy and paste all the contents of the included file.
Including .cpp file is not strictly disallowed, and sometimes good choice, it is considered a bad practice. As I mentioned, including file, means that all contents of the file are being duplicated at the place of inclusion. This is okay, for the header file with inclusion guard, but not okay for .cpp file, since every function definition inside this file, will be duplicated.
Not including file in the build script means, that only the those duplicated data are included in the build, otherwise you would end up with multiple function redefinition errors.
If you are looking for IDE, consider:
Visual Studio
Code Blocks
Eclipse
IDE won't do all the work, but you can be significantly more productive using good IDE.
TLDR:
Use inclusion guards
Include all .cpp files in build script.
Do not "#include" .cpp files.
In every .cpp file, include only needed headers, to reduce compilation time.
I see a lot of good suggestions with good practices but your mistake (including .cpp files from a .cpp file) suggest you're missing some concept in the C/C++ build process, I hope a little explanation would help you understand better and avoid the mistake.
Think of .c .cc .cxx .cpp files as modules, a .cpp file is a module, with your implementation of something, .h .hpp are just headers where usually you don't put implementations but declarations to be shared with multiple modules.
Usually each .cpp module is compiled to a binary object g++ -c -o mymod1.o mymod1.cpp then (once all modules are compiled) linked together g++ -o myprog mymod1.o mymod2.o ....
Even if you compile and link with a single command g++ -o myprog mymod1.cpp mymod2.cpp behind the scene g++ handle each module as single object.
I think is important you understand that each module/object know nothing about others, and if you need some other module (your main.cpp) to know something about mymod1.cpp a header file is required .h .hpp (mymod1.h) with the declarations needed to be shared: module global variables, defines, enums, function prototypes or class declarations, then just include mymod1.h in the module(s) where you want to use something of your mymod1 implementation (main.cpp).
Also, you write you're using a shell script to build, that's ok if your project are few files, better would be to use something like make, learn how to use it will require some time but then I bet geany have some facility to build projects based on Makefiles, make is the way to handle C/C++ projects from a long time.

Inclusion of only header files does not run the program

I am running my programs in Ubuntu using a library names cpt. I am including the required header files from the library but the program does not work because it is unable to access the functions in the header file. ( it shows that error ) I have to include the .cpp files as well which contains the function's complete definition.
I am now running my programs by also including the .cpp files associated with the required headers. Why I am getting this error inspite of the fact that I have -I/Desktop/cpt while compiling ?
You should not include .cpp files, they should be compiled, you should specify to your compiler which .cpp files to compile, and where to find the .h files the .cpp files require.
Maybe you should link your program with the corresponding shared/static library that provides the implementation of those functions declared in the header file?
Vladimirm is correct, you do not need to #include the .cpp files. the header files should compile with or without their associated .cpp files. All of the .cpp files are linked together in a process known as linking, but the header files provide prototypes for functions during linking.
Are you using angle brackets around your #include? e.g.
#include <mylib.h>
If so, you might consider switching to double quotes, e.g.
#include "mylib.h"
Depending on where the source files you are referencing are located, this could be the issue.

including header file in a separate folder

I created a class (say, myclass.h/cpp). I want to use the class from many different places. Therefore, I put those files in a folder (say, C:\cpp_include) and I want to include them from whatever folder my codes are. I have a code which uses the class (say, main.cpp). In main.cpp, I include myclass:
#include "myclass.h"
I compile using a .pro file and nmake. In the .pro file, I specify the folder as:
INCLUDEPATH += C:\cpp_include
When I compile the code using nmake, myclass.h is properly included, but myclass.cpp doesn't seem to be found by compiler.
When I specify myclass.cpp as one of the source files in .pro file:
SOURCES += main.cpp C:\cpp_include\myclass.cpp
The exe file is built correctly. But, I would like myclass.cpp file to be found automatically when myclass.h is included, i.e. without setting myclass.cpp as a source file. Would this be possible? It looks like that's what happens with classes from Qt and Qwt (e.g .h/cpp files in /src/ folder in Qt and Qwt). Am I missing somthing?
Thanks a lot!
Daisuke
A simple technique is to have build scripts (makefiles) in the cpp directories. Write a rule that traverses the directories, executing the build scripts. This one step in isolating functionality and also allows one to use libraries.
That's just not how it works. The .cpp is the file that matters, header files (.h) just get copied into the other .cpp files. Therefore you need to add the myclass.cpp to your sources for compiling. Or, if it's a library class, you could also compile it once into a static library (.lib) and just add that to your linker files. But you ultimately need to somehow include you implementation in the project where it's used.