I made a test project to let you can reproduce it.
- project tree
├─test
│ │ CMakeLists.txt
│ │ main.cpp
│ │
│ └─bbb
│ CMakeLists.txt
│
└─test_shared_lib
CMakeLists.txt
shared_lib_main.cpp
shared_lib_main.h
-
steps to produce
-
build shared library
run commands to build shard library
cd ${SHARED_LIBRARY_ROOT}
cmake -S . -B build
cmake --build build --config Debug
cmake --install build --prefix install/Debug --config Debug
cmake --build build --config Release
cmake --install build --prefix install/Release --config Release
- copy output to test dir
copy all content in install directory to test/bbb
and then copy test/bbb/Relase/include
or test/bbb/Relase/include
to test/bbb
- build test binary and run ctest
If all proceduces above are correct, the building should success.
use visual studio to open test CMakeLists.txt and then build it.
run tests from TestExplorer.
- files
# test
#
cmake_minimum_required (VERSION 3.28)
project (test_project
VERSION 0.0.1
LANGUAGES C CXX
)
add_subdirectory ("bbb")
include (CTest)
add_executable (test_main
"main.cpp"
)
target_compile_features (test_main
PRIVATE cxx_std_23
)
target_link_libraries (test_main
PRIVATE bbb::bbb
)
if (${BUILD_TESTING})
add_test (NAME normal_function_test_test_main
COMMAND test_main
)
set_tests_properties (normal_function_test_test_main
PROPERTIES
ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<SHELL_PATH:$<PATH:GET_PARENT_PATH,$<TARGET_PROPERTY:bbb::bbb,IMPORTED_LOCATION$<IF:$<CONFIG:Debug>,_DEBUG,_RELEASE>>>>"
)
endif()
// main.cpp
//
#include <shared_lib_main.h>
int main(int argc, char* argv[])
{
int out = 0;
int res = safe_add_two_integer(1, 2, &out);
if (res == 0)
{
return 1;
}
return 0;
}
# bbb
#
add_library (bbb SHARED IMPORTED GLOBAL)
set_target_properties (bbb PROPERTIES
IMPORTED_LOCATION_RELEASE "${CMAKE_CURRENT_LIST_DIR}/Release/lib/test_shared_lib.dll"
IMPORTED_IMPLIB_RELEASE "${CMAKE_CURRENT_LIST_DIR}/Release/lib/test_shared_lib.lib"
IMPORTED_LOCATION_DEBUG "${CMAKE_CURRENT_LIST_DIR}/Debug/lib/test_shared_lib.dll"
IMPORTED_IMPLIB_DEBUG "${CMAKE_CURRENT_LIST_DIR}/Debug/lib/test_shared_lib.lib"
IMPORTED_CONFIGURATIONS "Release;Debug"
MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release
MAP_IMPORTED_CONFIG_MINSIZEREL Release
)
target_include_directories (bbb
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include"
)
add_library (bbb::bbb ALIAS bbb)
install (IMPORTED_RUNTIME_ARTIFACTS bbb)
- test_shared_lib/CMakeLists.txt
# test
#
# build and install commands:
# > cd ${PROJECT_ROOT}
# > cmake -S . -B build
# > cmake --build build --config Debug
# > cmake --install build --prefix install/Debug --config Debug
# > cmake --build build --config Release
# > cmake --install build --prefix install/Release --config Release
cmake_minimum_required (VERSION 3.23)
project (test_shared_lib_project
VERSION 0.0.1
LANGUAGES C CXX
)
include (GenerateExportHeader)
add_library (test_shared_lib SHARED)
set_target_properties (test_shared_lib
PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON
)
generate_export_header (test_shared_lib)
target_sources (test_shared_lib
PRIVATE "shared_lib_main.cpp"
PUBLIC FILE_SET public_headers TYPE HEADERS FILES "shared_lib_main.h"
)
install (TARGETS test_shared_lib
RUNTIME DESTINATION lib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
FILE_SET public_headers DESTINATION include
)
install (FILES "${PROJECT_BINARY_DIR}/test_shared_lib_export.h" TYPE INCLUDE)
- test_shared_lib/shared_lib_main.cpp
// main.cpp
//
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
int safe_add_two_integer(int a, int b, int* out)
{
if (out == nullptr)
{
return 0;
}
if (a > ~b)
{
return 0;
}
*out = a + b;
return 1;
}
#ifdef __cplusplus
}
#endif // __cplusplus
- test_shared_lib/shread_lib_main.h
// shared_lib_main.h
//
#pragma once
#include "test_shared_lib_export.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
int TEST_SHARED_LIB_EXPORT safe_add_two_integer(int a, int b, int* out);
#ifdef __cplusplus
}
#endif // __cplusplus