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

Quick way to *only* include the implementation #1061

Closed
Leandros opened this issue Oct 19, 2017 · 12 comments
Closed

Quick way to *only* include the implementation #1061

Leandros opened this issue Oct 19, 2017 · 12 comments

Comments

@Leandros
Copy link

Consider the following case: I have a project, which is compiled using a precompiled header, in which I include catch.hpp, how would I now create Catch's main function (or pull in the required code to be compiled into my TU)?

A quick'n'dirty way would be to add a macro, just like CATCH_CONFIG_RUNNER, which will only pull in the implementation and nothing else.

@philsquared
Copy link
Collaborator

I'm not entirely sure what you're asking.
In terms of a macro that will "only pull in the implementation and nothing else" - why is CATCH_CONFIG_RUNNER not what you want?

But with precompiled headers you would only want the non-impl parts included (ie without CATCH_CONFIG_-anything (and it's debatable if you even need Catch in the pch - I've always excluded it).

Perhaps you could sketch in a little more?

@Leandros
Copy link
Author

CATCH_CONFIG_RUNNER doesn't work, since the catch.hpp header was already formerly included and the implementation is therefore guarded by the TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED macro.

The case is, that I want to include catch.hpp in my precompiled header, but still want only one single TU to contain the main implementation, which is currently not possible, due to the requirement that the precompiled header has to be the first to appear in any file.

@vadz
Copy link
Contributor

vadz commented Nov 1, 2017

Sorry, I didn't notice this before posting, but I have exactly the same problem. Maybe my description of it on the mailing list is more clear?

@horenmar
Copy link
Member

horenmar commented Nov 1, 2017

@vadz It is clear enough, but not supported yet.

As an ad-hoc solution, you can cut out everything guarded with CATCH_IMPL and CATCH_CONFIG_EXTERNAL_INTERFACES macro's into a separate header. Or define CATCH_CONFIG_EXTERNAL_INTERFACES in the PCH and cut out only the CATCH_IMPL part.

@vadz
Copy link
Contributor

vadz commented Nov 1, 2017

FWIW I ended up doing

#include "internal/catch_impl.hpp"
namespace Catch { namespace Clara { UnpositionalTag _; } }

in the PCH-generating file. Ugly but seems to work.

@GatoRat
Copy link

GatoRat commented Nov 9, 2017

When I faced this issue, I made my main source file as small as possible, included only catch.hpp and turned off pre-compiled headers for that file. (Still annoying, but it worked.)

@horenmar
Copy link
Member

horenmar commented Nov 9, 2017

I thought about this for a bit and I am going to implement it with some caveats

  1. Conditional include guards are non-obvious, so the main file will need to undef the include guard manually.
  2. The support will be experimental for a while, so no stability guarantees.
  3. The PCH will need to define a special macro. This is because we provide some pluggable parts controllable by defines, which the implementation file forces to be on, to avoid implementation complexity and weird link errors. Since we cannot use the same trick in a "PCH + impl only" context, it will be up to the PCH to provide proper configuration.
  4. I am not sure what to do about the "external interfaces" part of Catch. It is required for implementation to function, but users might want to have it in all headers (it provides access to listeners and reporter interfaces).

@horenmar
Copy link
Member

I made a proof of concept branch for this (dev-PCH-support) and it seems to work on a small scale, but I don't have any project handy to test it out on.

// stdafx.h
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#define CATCH_CONFIG_ALL_PARTS
#include "catch.hpp"
// PCH-test.cpp:
#include "stdafx.h"

#undef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED
#define CATCH_CONFIG_IMPL_ONLY
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
// tests1.cpp:
#include "stdafx.h"

TEST_CASE("FooBarBaz") {
    REQUIRE(1 == 2);
}

Can someone test the attached single header version and report back?

catch.zip

@Leandros
Copy link
Author

Leandros commented Nov 14, 2017

Works for me!

@vadz
Copy link
Contributor

vadz commented Nov 14, 2017

It seems to work for me too, I just had to replace CATCH_CONFIG_MAIN from above with CATCH_CONFIG_RUNNER, as I have my own main().

I'm still (and will continue to have to, in near future) using Catch 1.x though, I wonder if it could be backported there? Thanks!

@horenmar
Copy link
Member

@vadz I won't be backporting it, but I'd expect one should be able to do it with relative ease by looking at the commit diff.

horenmar added a commit that referenced this issue Nov 14, 2017
@horenmar horenmar added Resolved - pending review Issue waiting for feedback from the original author and removed In progress labels Nov 19, 2017
@horenmar
Copy link
Member

v2.1.0 is out and contains the PCH support

@horenmar horenmar removed the Resolved - pending review Issue waiting for feedback from the original author label Jan 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants