rapidcheck icon indicating copy to clipboard operation
rapidcheck copied to clipboard

Tests can't find rapidcheck/catch.h (its own header)

Open yurivict opened this issue 4 years ago • 7 comments

FreeBSD 12

yurivict avatar Oct 27 '19 17:10 yurivict

I've found this occur with <rapidcheck/gtest,h> as well on an initial build. Cleaning the project and building the whole thing a second time resolved this for me. I'm not quite sure why this has been happening. This (the project I'm working on) is the second project using Rapidcheck to have this issue come up but it doesn't seem to be particularly repeatable.

I'll try to better log what's going on in a few days but at the moment I'm a bit strapped on time.

OneDeuxTriSeiGo avatar Jul 27 '20 11:07 OneDeuxTriSeiGo

OK I've finally figured it out with the help of the brilliant folks on the CPPLang Slack.

On Modern versions of CMake (3.12+), when you set an option variable it can be a normal variable. However Rapidcheck is also targetting lower CMake versions. The legacy behaviour of CMP0077 causes option() to forcibly resets the values of any non-CACHE variables. The new behaviour doesn't reset the value of the variable if it is already set. This new behaviour is how most people expect option() to work.

There are two solutions as a user:

  1. set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) and force the new CMP0077 behaviour on all subprojects. This works great for the most part however it could potentially break things in other subprojects.

  2. Set the option variables with CACHE FORCE to properly enable the feature. This is option is the least likely to break something in another subproject.

@yurivict Try this and see if it fixes your issues.

OneDeuxTriSeiGo avatar Nov 07 '20 20:11 OneDeuxTriSeiGo

For now I chose a simpler solution: added -I${WRKSRC}/extras/catch/include to CXXFLAGS.

Now it finds the header, but fails later:

ld: error: unable to find library -lrapidcheck_catch

The library librapidcheck_catch wasn't built.

Additionally, the header in question rapidcheck/catch.h that tests use only exists in the source tree extras/catch/include/rapidcheck/catch.h and isn't installed. Is this expected?

yurivict avatar Nov 07 '20 22:11 yurivict

Sorry for not responding earlier, apparently I'm an idiot and never saw the notification.

That issue is going to appear due to that variable issue and I don't think there is any way around it. The following is a snippet from my personal code that shows how to implement the latter solution:

include(FetchContent)

set(RC_ENABLE_GTEST ON CACHE BOOL "Rapidcheck GTest Support" FORCE)
set(RC_ENABLE_GMOCK ON CACHE BOOL "Rapidcheck GMock Support" FORCE)
set(RC_ENABLE_BOOST ON CACHE BOOL "Rapidcheck Boost Support" FORCE)
set(RC_ENABLE_RTTI  ON CACHE BOOL "Rapidcheck RTTI Support"  FORCE)
FetchContent_Declare(rapidcheck
    GIT_REPOSITORY 
        "https://github.com/emil-e/rapidcheck.git"
    GIT_TAG        
       master
)

FetchContent_MakeAvailable(
    rapidcheck
)

This automatically fetches the dependencies and all but the set() commands on lines 3-6 show how to change the feature enable/disable to fix this issue with CMake not always building rapidcheck "extras" libraries. In your case, just replace the RC_ENABLE_GTEST line with RC_ENABLE_CATCH.

OneDeuxTriSeiGo avatar Feb 27 '21 01:02 OneDeuxTriSeiGo

I guess the better way to describe it is, replace the line where you enable RC_ENABLE_CATCH in CMake with the following:

set(RC_ENABLE_CATCH ON CACHE BOOL "Rapidcheck Catch2 Support" FORCE)

I'll try to keep better attention to my Github notifications so let me know if this works for you.

OneDeuxTriSeiGo avatar Feb 27 '21 01:02 OneDeuxTriSeiGo

set(RC_ENABLE_CATCH ON CACHE BOOL "Rapidcheck Catch2 Support" FORCE)

I have not had a chance to try this yet, however adding the following to my CMakeLists.txt did work:

add_subdirectory(rapidcheck)
add_executable(mytest main.cpp)
target_link_libraries(mytest PUBLIC Catch2::Catch2 rapidcheck INTERFACE rapidcheck_catch)
target_include_directories(mytest PUBLIC rapidcheck/extras/catch/include

This is definitely more hackey than some of the other proposed solutions, but I thought I would share it anyway in case it is helpful to someone.

ezzieyguywuf avatar Jun 29 '21 00:06 ezzieyguywuf

I was able to fix the problem with rapidcheck/catch.h using the flag -I${SRCDIR}/extras/catch/include.

But then it broke like this:

ld: error: unable to find library -lrapidcheck_catch

librapidcheck_catch.* isn't built at all.

yurivict avatar Jul 30 '22 07:07 yurivict