So I want to make a shared library, and testing how it works... But I just don't understand what am I doing wrong here...
main.cpp:
//main.cpp
#include <iostream>
__declspec(dllimport) int foo(int);
int main() {
std::cout << foo(5) << std::endl;
}
shared.cpp:
shared.cpp
__declspec(dllexport) int foo(int k) {
return k;
}
Every time I tried to build, it fails with the following output: output:
[build] MSBuild version 17.7.2+d6990bcfa for .NET Framework
[build]
[build] MnShared.vcxproj -> D:\Programs\tst\mn\build\Debug\MnShared.dll
[build] main.cpp
[build] main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl foo(int)" (__imp_?foo@@YAHH@Z) referenced in function main
[build] D:\Programs\tst\mn\build\Debug\MN.exe : fatal error LNK1120: 1 unresolved externals
[proc] The command: "E:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build d:/Programs/tst/mn/build --config Debug --target ALL_BUILD -j 10 -- exited with code: 1
[driver] Build completed: 00:00:02.045
[build] Build finished with exit code 1
NOTE: shared.cpp and main.cpp are in the same project and are getting built in the same directory
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(MN CXX)
add_executable(MN main.cpp)
add_library(MnShared SHARED shared.cpp)
Adding target_link_libraries(MN PRIVATE MnShared)
at the bottom of CMakeLists.txt
solved the problem.