I want to include a class from one project in another project.
I have a subdirs project that contains two sub projects, a windows qt console application and an autotest project to test the console application. My console application contains one class which I want to pull into my unit tests for testing:
Here's the header:
// calculator.h:
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator{
private:
public:
Calculator(int year);
int getYear(){ return 666; }
int getMonth();
int getDay();
};
#endif
Here's the source:
// calculate.cpp
"#include "calculator.h"
Calculator::Calculator(int year){}
int Calculator::getMonth(){
return 42;
}
int Calculator::getDay(){
return 3333;
}
Here's how my unit test looks:
//tst_foobar.cpp
#include <QtTest>
//#include "../Calculator/calculator.h"
#include "../Calculator/calculator.cpp"
// add necessary includes here
class Foobar : public QObject
{
Q_OBJECT
private slots:
void test_case1();
};
void Foobar::test_case1()
{
Calculator myCalc(42);
}
QTEST_APPLESS_MAIN(Foobar)
#include "tst_foobar.moc"
My problem is when I include the other subdir's header file like this: #include "../Calculator/calculator.h" it doesn't work properly. I cannot test any of the class functions defined in calculator.cpp. I can explicitly include calculate.cpp like this, #include "../Calculator/calculator.cpp" and my tests work as expected, but is this the proper way to do it?
I've never seen .cpp files included into files like this, only the header? But if only include the header, the header doesn't include the function definitions in calculator.cpp? Should my header file include the .cpp file? That way I could include just the header in other files like you often see in C++. By why then does the class generated by QT Creator do things the other way around? Creates a header file and a .cpp file, and the .cpp file is the one that includes the header??
Very new to C++ programming and a bit confused. Detailed help greatly appreciated.
Your questions are going to be answered as soon as you use CMake, so this might help.
All that boring part to find headers and cpp files are done within a CMakeLists.txt file. Then an executable file is created when you use add_executable()
Be aware that you will probably create a build directory to be "clean".
I think your CMakeLists.txt file is going to be something like
cmake_minimum_required(VERSION 2.8.11)
project(your_project_name)
enable_testing()
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Test REQUIRED)
add_executable(foo your_test.cpp your_src.cpp)
add_test(foo foo)
target_link_libraries(foo Qt5::Test)
You actually never include the cpp file.
If you include the h file you should be able to access the objects that are in the class.
When you include a .h file its basically saying copy whats in the .h file and paste it in the cpp. calculator.h is included in calculator.cpp so when the program compiles it will create binary file with both cpp and .h together. cpp files get compiled and linked together .h files dont since they are already going to be compiled with the .cpp. So in terms of main you just need to include the .h if you include .cpp it would not be efficient . **The reason why you also include the .h in main is so the program will know about the class. I hope that answers your question.
To sum things up: Include calculator.h in calculator.cpp and main.
Related
I am using CMake 3.16.
I add my source files to my target with target_sources(). I would like later in my CMakeLists.txt to remove a source file I previously added with target_sources().
For example:
target_sources(my_target PRIVATE main.c abc.c def.c ghi.c)
# Later...
# Remove def.c from the previously added source files.
Is there a way to do this ideally without setting a custom "sources" variable and removing it from this list with list(REMOVE_ITEM ...)?
EDIT:
The context of this question is unit testing static functions in C.
My program is made of a lot of static functions that I want to unit test. To test them, I decided to include the .c file in the test files instead of the .h.
For example:
abc.c:
#include "abc.h"
// several static functions defined here
abc.h:
// some stuff
test_abc.c:
#include "unity.h" // a unit test framework
#include "abc.c" // notice the .c instead of the .h to be able to test the static functions
// the test functions
By doing this, I need to remove in CMake the xxx.c file if I add the test_xxx.c file because otherwise the content of the xxx.c file will be defined 2 times and the linker will not be happy with it.
You can set the source file property HEADER_FILE_ONLY to ON on the source file in question. See also set_source_files_properties().
Since CMake version 3.18, you can also do this if the source file belongs to a target defined in a different directory:
set_source_files_properties(subdir/source.c TARGET_DIRECTORY target-from-subdir PROPERTIES HEADER_FILE_ONLY ON)
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.
I am currently trying to get my head around library linking with Qt in order to split up some existing code into logically structured modular parts that can be called by different applications.
I keep getting a problem where no obj files are being created... so I have created a smaller sample project and replicated my problem below.
It seems, to me, the problem lies in the library ...so I will start there.
If I have code like this, the obj file is created (and can therefore be linked to by my test application):
mylib.h
#ifndef MYLIB_H
#define MYLIB_H
#include "mylib_global.h"
class MYLIB_EXPORT MyLib
{
public:
MyLib();
~MyLib();
private:
};
#endif // MYLIB_H
mylib.cpp
#include "mylib.h"
MyLib::MyLib()
{
}
MyLib::~MyLib()
{
}
But if I remove the cpp file and just have the header file with function bodys included (as is the structure of my actual codes library as the code consists ONLY of templates), nothing is created:
mylib.h
// .......
public:
MyLib(){}
~MyLib(){}
// .......
How can I get Qt to build my code please?
If your library is just a header file, you don't need to compile it, include your headers in your other projects, and they will be compiled into it.
I'm trying to access functions from another file for use inside my class definition:
// math.cpp
int Sum(int a, int b){
return (a + b);
}
// my_class.cpp
#include <math.cpp>
#include <my_class.h>
int ComputeSomething() {
...
return ::Sum(num1, num2);
}
Despite my best efforts, I can't get the compiler to spit out anything outside the likes of ::Sum has not been declared or Sum was not declared in this scope.
I'm trying to wrap my head around code organization in C++, any help appreciated.
It might be worth noting that I'm programming for Arduino.
To be able to access functions from a user-defined library, best divide that library into a .h (or .hpp) and a .cpp file. I understand you have actually done this, but tried various options – among them the inclusion of the .cpp file – for the sake of finding a solution.
Still, to ensure things work as expected, the declarations of functions and classes should go into the .h file, best protected by something like
#ifndef MY_H_FILE
#define MY_H_FILE
/* ..Declarations.. */
#endif
Then to include the .h file (I'll assume it's named my.h), either use
#include "my.h" // path relative to build directory
or
#include <my.h> // path relative to any of the include paths
The latter only works if my.h is found on an include path previously known to the compiler (e.g. what is specified using the -I command line option in GCC). The former works if the path to the .h file given is relative to the directory your are building from.
Finally, do not use a file name that can be confused with a system library (such as "math.h"), especially if you are using the <...> syntax, as the include path will definitely include the system library header files.
Have you followed the instructions given here?
User-created libraries as of version 0017 go in a subdirectory of your
default sketch directory. For example, on OSX, the new directory would
be ~/Documents/Arduino/libraries/. On Windows, it would be My
Documents\Arduino\libraries. To add your own library, create a new
directory in the libraries directory with the name of your library.
The folder should contain a C or C++ file with your code and a header
file with your function and variable declarations. It will then appear
in the Sketch | Import Library menu in the Arduino IDE.
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.