cpputest/cpputest

Linking CppUTestExt

Closed this issue · 1 comments

Hi,
I'm having problems when trying to use Mock Support.
My project is built with CMake. If I don't use Mock Support and stick only
to CppUTest, everything works great. If I try to add Mock Support and call mock() ,
I'm getting link errors.

Relevant part of CMakeLists.txt:

project (myProject)

add_definitions(-DCPPUTEST_USE_EXTENSIONS=Y)

# Irrelevant stuff here

# CppUTest
include(FetchContent)
FetchContent_Declare(
    CppUTest
    GIT_REPOSITORY https://github.com/cpputest/cpputest.git
    GIT_TAG        master # or use release tag, eg. v4.0
)
# Set this to ON if you want to have the CppUTests in your project as well.
set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)

add_executable(RunAllTests RunAllTests.cpp)

target_link_libraries(
    RunAllTests 
    pthread
    CppUTest::CppUTest
    CppUTest::CppUTestExt

    # Other test files here...
)

With this CMakeLists.txt, the project builds great and all the tests run successfully.
But when I try to add mock() to any test file it fails to build due to linking errors.
Here is an example test file that breaks my build:

#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"

extern "C"
{
	#include <stdint.h>
}

TEST_GROUP(ttt)
{
    void teardown() {
        mock().clear();
    }
};

void testm() {
    mock().actualCall("testm");
}

TEST(ttt, whyDontYouBuild)
{   
    mock().expectOneCall("testm");
    testm();
    mock().checkExpectations();
}

I get this error:
/usr/bin/ld: rx_measureTest.cpp:(.text+0x313): undefined reference to `mock(SimpleString const&, MockFailureReporter*)'

Any help is appreciated. Thanks.

To anyone who may stumble upon this in the future:
For some reason, while CppUTest works if you add your tests as libraries and link RunAllTests.cpp against them, CppUTestExt doesn't.
So, instead of:

cmake_minimum_required(VERSION 3.22)

project (myProject)

add_definitions(-DCPPUTEST_USE_EXTENSIONS=Y)

add_library(tttTest tttTest.cpp)

# CppUTest
include(FetchContent)
FetchContent_Declare(
    CppUTest
    GIT_REPOSITORY https://github.com/cpputest/cpputest.git
    GIT_TAG        master # or use release tag, eg. v4.0
)
# Set this to ON if you want to have the CppUTests in your project as well.
set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)

add_executable(RunAllTests RunAllTests.cpp)

target_link_libraries(
    RunAllTests 
    CppUTest::CppUTest
    CppUTest::CppUTestExt
    pthread

    tttTest
)

I did:

cmake_minimum_required(VERSION 3.22)

project (myProject)

add_definitions(-DCPPUTEST_USE_EXTENSIONS=Y)

# CppUTest
include(FetchContent)
FetchContent_Declare(
    CppUTest
    GIT_REPOSITORY https://github.com/cpputest/cpputest.git
    GIT_TAG        master # or use release tag, eg. v4.0
)
# Set this to ON if you want to have the CppUTests in your project as well.
set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)

add_executable(RunAllTests RunAllTests.cpp tttTest.cpp)

target_link_libraries(
    RunAllTests 
    CppUTest::CppUTest
    CppUTest::CppUTestExt
    pthread
)

Now both libraries link fine.