I have a GUI app project with the following structure
C:\PROJECT
| CMakeLists.txt
|
+---lib
| \---googletest
|
+---src
| CMakeLists.txt
| Base.h
| Data.h
| Math.h
| Tools.cpp
| Tools.h
| project.cpp
| project.h
|
\---tests
CMakeLists.txt
tests.cpp
my_test1.cpp
main CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project ("project")
set(CMAKE_CXX_STANDARD 17)
set(WXWIN "C:/msys64/wxWidgets")
# =========================================================================
#### add libraries in subdirectories
set(BUILD_DEPS ON)
set(INCLUDE_LIBS "lib")
list(APPEND CMAKE_MODULE_PATH ${INCLUDE_LIBS})
set(wxWidgets_ROOT_DIR ${WXWIN})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows")
find_package(wxWidgets COMPONENTS net gl core base)
include(${wxWidgets_USE_FILE})
set(wxWidgets_USE_UNICODE ON)
# =========================================================================
#### Include Project Directories and Subdirectories
include_directories(${INCLUDE_LIBS})
add_subdirectory(src)
add_subdirectory(lib/googletest)
add_subdirectory(tests)
src CMakeLists.txt
# =========================================================================
#### Create shared library for the project
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(BINARY ${CMAKE_PROJECT_NAME})
set(PROJECT_LINK_LIBS ${CMAKE_PROJECT_NAME}_lib)
set(LIBRARY_SRC Tools.cpp)
add_library(${PROJECT_LINK_LIBS} SHARED ${LIBRARY_SRC})
set_target_properties(${PROJECT_LINK_LIBS} PROPERTIES POSITION_INDEPENDENT_CODE 1)
# =========================================================================
#### Create main executable of the project
add_executable (${BINARY} WIN32 "project.cpp" "project.h")
target_compile_features(${BINARY} PUBLIC cxx_std_17)
target_link_libraries (${BINARY} ${PROJECT_LINK_LIBS} ${wxWidgets_LIBRARIES})
tests CMakeLists.txt
set(BINARY ${CMAKE_PROJECT_NAME}_tst)
file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.h *.cpp)
add_executable(${BINARY} ${TEST_SOURCES})
target_compile_features(${BINARY} PRIVATE cxx_std_17)
add_test(NAME ${BINARY} COMMAND ${BINARY})
target_link_libraries(${BINARY} ${CMAKE_PROJECT_NAME}_lib gtest)
If I try to build this project I get linking error
[16/18] Linking CXX executable tests\project_tst.exe
FAILED: tests/project_tst.exe
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=tests\CMakeFiles\project_tst.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MIB055~1\2019\COMMUN~1\VC\Tools\MSVC\1427~1.291\bin\Hostx64\x64\link.exe /nologo tests\CMakeFiles\project_tst.dir\test_dummy.cpp.obj tests\CMakeFiles\project_tst.dir\tests.cpp.obj /out:tests\project_tst.exe /implib:tests\project_tst.lib /pdb:tests\project_tst.pdb /version:0.0 /machine:x64 /subsystem:windows /debug /INCREMENTAL /subsystem:console src\project_lib.lib lib\gtestd.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\MIB055~1\2019\COMMUN~1\VC\Tools\MSVC\1427~1.291\bin\Hostx64\x64\link.exe /nologo tests\CMakeFiles\project_tst.dir\test_dummy.cpp.obj tests\CMakeFiles\project_tst.dir\tests.cpp.obj /out:tests\project_tst.exe /implib:tests\project_tst.lib /pdb:tests\project_tst.pdb /version:0.0 /machine:x64 /subsystem:windows /debug /INCREMENTAL /subsystem:console src\project_lib.lib lib\gtestd.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:tests\CMakeFiles\project_tst.dir/intermediate.manifest tests\CMakeFiles\project_tst.dir/manifest.res" failed (exit code 1169) with the following output:
C:\msys64\home\igor\project.minimal\out\build\x64-Debug (default)\gtestd.lib(gtest-all.cc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in test_dummy.cpp.obj
............................................................................................many lines...
C:\msys64\wxWidgets\include\wx\wxcrt.h(924): warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\msys64\wxWidgets\include\wx\wxcrt.h(925): warning C4996: '_wgetenv': This function or variable may be unsafe. Consider using _wdupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\msys64\wxWidgets\include\wx\filefn.h(443): warning C4996: '_wopen': This function or variable may be unsafe. Consider using _wsopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
ninja: build stopped: subcommand failed.
what do I do wrong? Why do I get this error and how to fix it?
If I comment out add_subdirectory(tests)
from the main CMakeLists.txt, then build is successful.
But why does it fail if I add the tests subdirectory?