Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What if built doctest as static library instead of header-only #408

Closed
zchrissirhcz opened this issue Jul 31, 2020 · 2 comments
Closed

What if built doctest as static library instead of header-only #408

zchrissirhcz opened this issue Jul 31, 2020 · 2 comments

Comments

@zchrissirhcz
Copy link

Just feeling everytime including doctest.h is too large for generated .o fille.

Browsing the official CMakeLists.txt, feeling too complicated.

What if I just use doctest like the following? What features/details will be affected, comparing to using the default provided doctest.h ?

cmake_minimum_required(VERSION 3.15)

project(demo)

add_library(doctest STATIC
    doctest/doctest_fwd.h
    doctest/doctest.cpp
)
target_compile_definitions(doctest PRIVATE -DDOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)

add_executable(demo
    src/main.cpp
)
target_link_libraries(demo doctest)
target_include_directories(demo
    PUBLIC "${CMAKE_SOURCE_DIR}/doctest"
)

src/main.cpp

#include "doctest_fwd.h"

int factorial(int number) { 
    return number > 1 ? factorial(number - 1) * number : 1;
}

TEST_CASE("testing the factorial function") {
    CHECK(factorial(0) == 1);
    CHECK(factorial(1) == 1);
    CHECK(factorial(2) == 2);
    CHECK(factorial(3) == 6);
    CHECK(factorial(10) == 3628800);
}
@onqtam
Copy link
Member

onqtam commented Jul 31, 2020

This is perfectly fine, but it should be identical to just using the doctest.h header directly - by default including doctest.h is the same as including just doctest_fwd.h and the generated .o files should be the same.

Also the default cmake files provide a static library you can directly link to: doctest_with_main

@zchrissirhcz
Copy link
Author

@onqtam Thanks for reply.

Let me make it more clear, I guess puting doctest.h in my main.cpp will cause that, each time main.cpp is compilied (not linking), the functions/classes inside doctest.h will be compiled again. Not very clear about how much time it will cost, but I think it may save time. Also considering that there are more .cpp files in my project, then I would like to keep the compilation output as small as possible. Thus, I thought using doctest as an library would be more suitable for me.

Yes, including doctest.h, the header-only manner, will be suitable for many other situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants