Qt proper project structure for testing - c++

I am currently in the process of setting up QTests for my Qt project and I am struggling a bit to come up with a nice structure which fullfills my needs.
Atm the project looks like this:
.
├── keygen
│   ├── main.cpp
│   ├── tests
│   │   ├── passkeygen_tests.cpp
│   │   └── tests.pri
├── common.pri
├── MainApp
│   ├── mainapp.pro
│   ├── main.cpp
├── TestProject.pro
├── lib_utils
│   ├── utils.cpp
│   ├── lib_utils.pro
The TestProject is a subproject pro file containing info about keygen(standalone executable), MainApp and libutils (used by MainApp). As you can see I already added tests for the keygen according to what I saw in this example https://github.com/kelvins/CodeCoverageExampleQt/tree/master/CodeCoverageExample because I want to see the testcoverage of my subprojects. So the files look like this:
keygen.pro:
include($$PWD/../common.pri)
QT += core dbus testlib
TEMPLATE = app
DESTDIR = $${OUT_PWD}/bin
TARGET = keygen
include($$PWD/tests/tests.pri)
QMAKE_CXXFLAGS += --coverage
QMAKE_LFLAGS += --coverage
tests.pri:
SOURCES += $$PWD/passkeygen_tests.cpp
passkeygen_tests.cpp:
#include <QtTest>
#include "passkeygenerator.h"
class PasskeygenTester : public QObject
{
Q_OBJECT
private:
PasskeyGenerator passkeygen;
private slots:
void checkGeneration();
void checkGeneration_data();
};
//Implementations
static PasskeygenTester passkeygenInstance;
#include "passkeygen_tests.moc"
This works in the sense that when I compile the project I see the *.gcno files are generated, but this way I can not execute the tests standalone to get the actual coverage. So my question is what is the best way to do generate gcov information and have standalone executable tests? The link from earlier only cared about executing the test not runnability of the actual project.
I created a sample project which highlights the struggle I am facing especially when it comes to tests for a library:https://github.com/faxe1008/TestCoverageStructureTest

Related

How to configure an AzerothCore module to link with the external library?

I am developing a module, which depends on another library(wasmtime). I put files into:
modules/mod_wasm/src/include - header files, and
modules/mod_mine/src/lib/libwasmtime.a - the compiled library.
The problem which I faced is that when I compile the acore server with
./acore.sh compiler all
it gives me the error:
[100%] Linking CXX executable worldserver
/usr/bin/ld: ../../../modules/libmodules.a(ModWasm.cpp.o): in function `readWasmFile(char const*)':
ModWasm.cpp:(.text+0x63): undefined reference to `wasm_byte_vec_new_uninitialized'
/usr/bin/ld: ModWasm.cpp:(.text+0xce): undefined reference to `wasm_byte_vec_delete'
The question is it required somehow add to a config that library? If yes, then how to do that?
I was testing my code in simple main.cpp file and it was working with options like "-L${workspaceFolder}/lib" and "-lwasmtime".
Maybe, these options are also required for my module?
Here is a link to azerothcore project which I use.
my module locates in modules/mod-wasm folder
azerothcore-wotlk/modules ‹master*› » tree -L 3 mod-wasm
mod-wasm
├── CMakeLists.txt
├── LICENSE
├── Makefile
├── README.md
├── conf
│   ├── conf.sh.dist
│   └── wasm.conf.dist
├── include.sh
├── mod-wasm.cmake
├── setup_git_commit_template.sh
├── src
│   ├── ModWasm.cpp
│   ├── include
│   │   ├── doc-wasm.h
│   │   ├── wasi.h
│   │   ├── wasm.h
│   │   ├── wasmtime
│   │   ├── wasmtime.h
│   │   └── wasmtime.hh
│   ├── lib
│   │   ├── libwasmtime.a
│   │   └── libwasmtime.so
│   └── wasm_loader.cpp
└── wasm_modules
└── rust_wasm_app.wasm
As I understood from the logs what I see and because CMakeList.txt exists in modules folder, the project considers the folder as module. Which in its turn scans subdirs for *.cmake files and configures the project.
The question now is how to properly configure my module to show that it contains the compiled library wasmtime inside src/lib folder?
As I understood, I could use target_link_libraries, but it requires a target name, and I have no idea what it should be and where I can take it.
At the end, I was able to find an answer with try and catch.
Azerothcore modules supports modname.cmake file to be run when configure libmodules.a which contains all extra modules(if I understood it correctly.
this is part of modules/CMakeFiles.txt
# Enables Devs to Include a cmake file in their module that will get run inline with the config.
foreach(SOURCE_MODULE ${MODULES_MODULE_LIST})
message("SOURCE_MODULE: ${SOURCE_MODULE}")
include("${CMAKE_SOURCE_DIR}/modules/${SOURCE_MODULE}/${SOURCE_MODULE}.cmake" OPTIONAL)
endforeach()
here I have my dirty cmake file which allow me to compile the server
set(WASM_MODULE_DIR ${CMAKE_SOURCE_DIR}/modules/${SOURCE_MODULE})
set(WASM_MODULE_SRC_DIR ${WASM_MODULE_DIR}/src)
message("--------------------->>>>> APPLICATION_NAME : ${APPLICATION_NAME}")
message("--------------------->>>>> APP_PROJECT_NAME : ${APP_PROJECT_NAME}")
message("--------------------->>>>> SOURCE_MODULE : ${SOURCE_MODULE}")
message("--------------------->>>>> WASM_MODULE_DIR : ${WASM_MODULE_DIR}")
message("--------------------->>>>> WASM_MODULE_SRC_DIR : ${WASM_MODULE_SRC_DIR}")
# include wasmtime
target_include_directories(modules PUBLIC ${WASM_MODULE_SRC_DIR}/include)
target_link_directories(modules PUBLIC ${WASM_MODULE_SRC_DIR}/lib)
find_library(LIBWASMTIME_TO_INCLUDE NAMES wasmtime PATHS ${WASM_MODULE_SRC_DIR}/lib REQUIRED)
message("--------------------->>>>>>>>> LIBWASMTIME_TO_INCLUDE: ${LIBWASMTIME_TO_INCLUDE}")
target_link_libraries(modules PUBLIC wasmtime)
So, it compiles now.
But I have next problem, which I am trying to resolve. but this is another story.
Thank you all for the help

CMake creating libraries that depend each other

my goal is to create libraries like client and generator and use them in src/main.cpp, but sometimes these libraries depend each other.
In this case: client/User.hpp uses generator/IdGenerator.hpp
Project
│
├── CMakeLists.txt
├── libs
│   ├── CMakeLists.txt
│   ├── client
│   │   ├── CMakeLists.txt
│   │   ├── User.cpp
│   │   └── User.hpp
│   └── generator
│   ├── CMakeLists.txt
│   ├── IdGenerator.cpp
│   ├── IdGenerator.hpp
│   └── Types.hpp
└── src
└── main.cpp
Project/CMakeLists.txt:
cmake_minimum_required (VERSION 3.8)
project(game-project VERSION 0.1.0)
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_FLAGS "-Wall -Wextra -O0 -std=c++20")
add_executable (game src/main.cpp)
add_subdirectory(libs)
target_link_libraries(game libclient libgenerator)
libs/CMakeLists.txt:
add_subdirectory(generator)
add_subdirectory(client)
libs/client/CMakeLists.txt:
add_library(libclient STATIC
User.cpp
User.hpp
)
include_directories(generator/)
target_link_libraries(libclient libgenerator)
libs/generator/CMakeLists.txt:
add_library(libgenerator STATIC
IdGenerator.cpp
IdGenerator.hpp
Types.hpp
)
C++ files:
main.cpp:
#include <client/User.hpp>
int main(int argc, const char* argv[])
{
User user;
return 0;
}
client/User.hpp:
#pragma once
#include <generator/IdGenerator.hpp>
class User
{
Identifier id = IdGenerator::generateId();
};
When I run make after cmake, I get this error:
In file included from Project/libs/client/User.cpp:1:
Project/libs/client/User.hpp:3:10: fatal error: generator/IdGenerator.hpp: No such file or directory
3 | #include <generator/IdGenerator.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Sorry for the verbose summary, I was able to shorten the example this much. What do you think the problem is?
This question might seem like a duplicate of CMake libraries that depend on each other but as you can see I'm already applying the
include_directories(generator/).
I misunderstood what the problem was initially, but now I think this might help.
Try adding to the CMakeLists.txt of your client instead of
include_directories(generator/)
this command
target_include_directories(libclient PUBLIC <Path to your generator file>)
Maybe you need to experiment a little to get the path correct as you might need to either specify it from the root directory or the current one (where this CMakeLists.txt resides), but it might solve your problem.

C++ library public interface as single header file

This is an example of a tuigraphics library I'm working on, what would be the best approach into creating it's public interface accessible via a single header file?
├── CMakeLists.txt
├── include
│   ├── Cell.hpp
│   ├── Grid.hpp
│   ├── math.hpp
│   ├── Node.hpp
│   ├── Renderer.hpp
│   ├── shape
│   │   ├── CircleShape.hpp
│   │   ├── RectangleShape.hpp
│   │   └── Shape.hpp
│   └── types.hpp
└── src
└── Node.cpp
I would like to access the library like so:
#include <tuigraphics.hpp>
int main(void)
{
Node node(CircleShape(10), Vector2(10, 10));
return EXIT_SUCCESS;
}
I have thought about putting a header file tuigraphics.hpp including all header files, but I'm not quite sure about it. I have never built a static library, and i would like some advice on how to proceed. Also here the CMake configuration:
project(libex)
cmake_minimum_required(VERSION 3.16)
add_library(tuigraphics STATIC)
target_include_directories(tuigraphics PRIVATE ./include)
target_sources(tuigraphics PRIVATE src/Node.cpp
include/Node.hpp
include/types.hpp
include/math.hpp
include/Cell.hpp
include/shape/CircleShape.hpp
include/shape/Shape.hpp
include/shape/RectangleShape.hpp
include/Renderer.hpp)
target_include_directories(tuigraphics PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

Decrease the length of headers with help of target_include_directories

I have a project
├── CMakeLists.txt
│   ├── log
│   │   ├── CMakeLists.txt
│   │   ├── include
│   │   │   ├── log.h
│   │   └── src
│   │   ├── log.cpp
│   └── main.cpp
In log.cpp I am usng #include "../include/log.h" and in main.cpp I amd using #include "include/log.h"
I want to use #include "log.h"
I read that target_include_directories can help me.
How can I apply it to my CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(Logger)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
#include_directories(log/include/) -- I used this, but I want to use target_include_directories
add_library(log_lib log/src/log.cpp)
add_executable(demo main.cpp)
target_link_libraries(demo log_lib)
define the target and then use the name as the first argument for target_include_directories
add_library(log_lib log/src/log.cpp)
target_include_directories(log_lib PUBLIC log/include)
worry about INTERFACE vs PUBLIC vs PRIVATE after you've got it all working and you want to understand it better. (This option transitively affects targets that depend on your library).
The line below helped
target_include_directories(log_lib PUBLIC log/include)

Libclang don't follow include statement

I'm trying to use Libclang to programatically analyse the Opencv library, but when I try to import the main header from Opencv opencv.hpp, libclang won't follow the path.
Previously, it was reading everything quite beautifully, but then I figured it was following my $PATH's headers, and I want it to follow these specifics ones.
opencv.hpp is a file containing lots of #include statements like so:
#include "core/core_c.h"
#include "core/core.hpp"
#include "flann/miniflann.hpp"
// ... and so on
but, when I try to open it with libclang, or either clang ./opencv.hpp, it won't follow:
clang ./Header_Example/opencv.hpp
./Header_Example/opencv.hpp:46:10: fatal error: 'core/core_c.h' file not found
#include "core/core_c.h"
^
1 error generated.
but I'm sure it is on the right directory (a bit of my tree output):
── Header_Example
│   ├── opencv.hpp
│   ├── opencv2
│   │   ├── # more directories
│   │   ├── core
│   │   │   ├── affine.hpp
│   │   │   ├── core.hpp
│   │   │   ├── core_c.h
│   │   │   ├── types_c.h
│   │   │   ├── version.hpp
│   │   │   └── wimage.hpp
maybe I'm not using the right clang parameters?
A bit of context: I want to analyse Opencv types, classes and functions, this info is present on the headers, so I don't think I would need the full library to read the code. When I tried to use the full library I found myself in trouble. I need to use the iOS compilation(?)version(?) of the library, hence I copy/pasted the headers from the compiled version into this working directory (sorry? :) )
EDIT 1: It may seem odd my directories having this opencv2/, but if I remove the headers from inside of it, clang will complain 'bout why aren't they there: fatal error: 'opencv2/core/types_c.h' file not found
I simply had to add -I. before adding -I./opencv2 because I had one file one the same directory as opencv.hpp. Gosh I feel so stupid for missing this.