Search code examples
c++ccmakegoogletest

GoogleTest CMake linking undefined function


I'm using Clion and I've set up GoogleTest with help from this post here:
Setup Google test in CLion

Finally got everything to work except that I can't seem to be able to call any function from another file, I keep getting "undefined reference to" errors. I don't understand what I'm missing, all the files seem to be linked correctly.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(ComplexNumber)
add_subdirectory(src)
add_subdirectory(test)

src/CMakeLists.txt:

set(core_SRCS main.c ComplexNumber.c)
add_library(core ${core_SRCS})
add_executable(exe main.c)
target_link_libraries(exe core)

test/CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)

project(Test)

project(Example)

include(CTest)
enable_testing()

#set(gtest_disable_pthreads on) #needed in MinGW
include(F:/Programming/C/AYED/ComplexNumber/.repo/DownloadProject/DownloadProject.cmake)
download_project(
        PROJ                googletest
        GIT_REPOSITORY      https://github.com/google/googletest.git
        GIT_TAG             master
        UPDATE_DISCONNECTED 1
)

add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)

set(test_SRCS Test.cpp)
add_executable(runUnitTests ${test_SRCS})
target_link_libraries(runUnitTests gtest gmock core)
add_test(runUnitTests runUnitTests)

test/Test.cpp:

#include <gtest/gtest.h>
#include "../src/ComplexNumber.h"

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

TEST(ComplexNumberTest, positives){
    ComplexNumber complexNumber1 = {5, -2};
    ComplexNumber complexNumber2 = {7, 6};
    ComplexNumber result = {12, 4};
    ComplexNumber complexNumber3 = *sum(&complexNumber1, &complexNumber2);
    EXPECT_EQ(complexNumber3.realNumber, result.realNumber);
}

Error Message:

CMakeFiles\runUnitTests.dir/objects.a(Test.cpp.obj): In function `ZN32ComplexNumberTest_positives_Test8TestBodyEv':
F:/Programming/C/AYED/ComplexNumber/test/Test.cpp:14: undefined reference to `sum(ComplexNumber*, ComplexNumber*)'
collect2.exe: error: ld returned 1 exit status
Test\CMakeFiles\runUnitTests.dir\build.make:99: recipe for target 'Test/runUnitTests.exe' failed
mingw32-make.exe[3]: *** [Test/runUnitTests.exe] Error 1
CMakeFiles\Makefile2:1106: recipe for target 'Test/CMakeFiles/runUnitTests.dir/all' failed
mingw32-make.exe[2]: *** [Test/CMakeFiles/runUnitTests.dir/all] Error 2
CMakeFiles\Makefile2:1118: recipe for target 'Test/CMakeFiles/runUnitTests.dir/rule' failed
mingw32-make.exe[1]: *** [Test/CMakeFiles/runUnitTests.dir/rule] Error 2
mingw32-make.exe: *** [runUnitTests] Error 2
Makefile:551: recipe for target 'runUnitTests' failed

Solution

  • A few comments, which I think will end up helping you solve your problem

    1)

    When creating a library with add_library, it is good practice to precise what kind of library this will be:

    add_library(core STATIC ${core_SRCS})
    

    2)

    It is not a good idea to put a main source file in a library. Libraries should not contain the main function, otherwise what will happen, as in your case, when you have two main functions in your executable? Therefore, src/CMakeLists.txt should be:

    set(core_SRCS ComplexNumber.c)
    add_library(core ${core_SRCS})
    add_executable(exe main.c)
    target_link_libraries(exe core)
    

    3)

    Make sure that sum(ComplexNumber*, ComplexNumber*) is defined in ComplexNumber.c.