[CMake] How to link shared lib on windows (visual studio 2014).

Chaos Zhang zcsd2012 at gmail.com
Mon Apr 25 21:01:25 EDT 2016


Hi,

I faced this problem when i try built a shared lib  and linked it on
windows. The structure of my project as below:
-test_dir/
     -sub_hello/
           CMakeLists.txt
           hello.c
     -top/
           main.c
           CMakeLists.txt
     CMakeLists.txt

The content of each file:
①test_dir/CMakeLists.txt:
PROJECT(TESTLIB)
add_subdirectory(sub_hello sub_hello)
add_subdirectory(top top) 

②test_dir/sub_hello/CMakeLists.txt:
message("message from sub_hello")
add_library(hello_lib  SHARED hello.c)

③test_dir/top/CMakeLists.txt:
include_directories(../sub_hello)
add_executable(main main.c)
target_link_libraries(main hello_lib)

④test_dir/sub_hello/hello.c:
#include <stdio.h>
void HelloFunc()
{
	printf("###hello###\n");
}

⑤test_dir/top/main.c:
int main()
{
	HelloFunc();
	return 0;
}

After i cmake this project, generated .sln and .proj files, then i built it
and i get an error in vs: 

Error	LNK1104	can't open file "..\sub_hello\Debug\hello_lib.lib"

In folder ..\sub_hello\Debug\ , there was not a hello_lib.lib existed.
Then i look thorough and found a solution: 

Add "set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES)" in file
test_dir/sub_hello/hello.c  

Then i built this solution again, this time it success, and hello_lib.lib
and main.exe was generated. But when i try to run main.exe, an error 
occured: "lose hello_lib.dll". And i moved hello_lib.dll into the folder of
main.exe, and it worked well.

There are two questions i could not figure out:
①Is this "..\sub_hello\Debug\hello_lib.lib" associates with
"..\sub_hello\Debug\hello_lib.dll"? For windows can not use .dll directly,
and use a .lib to record the .dll's  entrance and location.
②How to solve the problem of main.exe can not find .dll file.

Best regards,
Chao Zhang





--
View this message in context: http://cmake.3232098.n2.nabble.com/How-to-link-shared-lib-on-windows-visual-studio-2014-tp7593346.html
Sent from the CMake mailing list archive at Nabble.com.


More information about the CMake mailing list