1

I am writting a C++/CLI project, using CMake as a build generator.

In the root of the project I have the main CMakeLists.txt, the include and the src dir.

# CMakeList.txt :
cmake_minimum_required (VERSION 3.8)

set(This Project) # Creates a variable to reference the project's name
project(${This} CXX) # Sets the name of the project and the languages used in

set(CMAKE_CXX_STANDARD 17) # This project will be build using the C++ 17 standard

# Directories for headers and source code
file(
    GLOB_RECURSE
        Headers
            ./include/Project/**.hpp
        Sources
            src/**.cpp
)

add_library(
    ${This} STATIC ${Headers} ${Sources}
) 

# Compiler options
SET_TARGET_PROPERTIES(${This} PROPERTIES COMPILE_FLAGS "/clr")
STRING(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
STRING(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clr")  
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /c")

# Testing
enable_testing() # Telling CMake that we will have unit tests
add_subdirectory(test) # Project tests
#target_link_libraries(${This} PRIVATE ProjectTests)

Then I have a subdirectory with the test package, using the CppUnitTestFramework

cmake_minimum_required(
    VERSION 3.8
)

set(This ProjectTests)
project(${This} CXX)

# Directories for the testing files of the projects
file(
    GLOB_RECURSE
        Sources
            src/**.cpp
)

add_executable(${This} ${Sources})

# The locations of the MS CppUnitTestFramework
include_directories("C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/VS/UnitTest/include")
link_directories("C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/VS/UnitTest/lib/x86/Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib")

target_link_libraries(${This} PUBLIC Project)

add_test(
    NAME ${This}
    COMMAND ProjectTests
)

I am not being able to make it work the test subdirectory with any test framework

Error:

Error   LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)\MSVCRTD.lib(exe_main.obj)  1   

``


Can someone give me a hand? Thanks in advice
Pyzyryab
  • 503
  • 2
  • 12
  • https://cmake.org/cmake/help/latest/prop_tgt/COMMON_LANGUAGE_RUNTIME.html might help – Alan Birtles Jan 16 '22 at 03:08
  • Thanks @AlanBirtles but that's not resolve my issue linking the test subdirectory with the principal library one – Pyzyryab Jan 16 '22 at 10:11
  • Even if you want to link with shared library (`.dll`), on Windows you still need `.lib` file. This file contains symbols, exported from your library. But if no symbols are exported, no `.lib` file is created. – Tsyvarev Jan 16 '22 at 10:29
  • If I follow the suggested duplicate @Tsyvarev I am ending in this: `Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) \MSVCRTD.lib(exe_winmain.obj) 1 ` – Pyzyryab Jan 16 '22 at 11:13
  • The command `link_directories` specifies **directories** where libraries will be searched. It doesn't link a library. **Linking** with a library is performed via `target_link_libraries` command. See that question: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library – Tsyvarev Jan 16 '22 at 12:28

0 Answers0