<p dir="ltr">Add __declspec(dllexport) before void HelloFunc()</p>
<div class="gmail_quote">On Apr 25, 2016 6:01 PM, "Chaos Zhang" <<a href="mailto:zcsd2012@gmail.com">zcsd2012@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I faced this problem when i try built a shared lib  and linked it on<br>
windows. The structure of my project as below:<br>
-test_dir/<br>
     -sub_hello/<br>
           CMakeLists.txt<br>
           hello.c<br>
     -top/<br>
           main.c<br>
           CMakeLists.txt<br>
     CMakeLists.txt<br>
<br>
The content of each file:<br>
①test_dir/CMakeLists.txt:<br>
PROJECT(TESTLIB)<br>
add_subdirectory(sub_hello sub_hello)<br>
add_subdirectory(top top)<br>
<br>
②test_dir/sub_hello/CMakeLists.txt:<br>
message("message from sub_hello")<br>
add_library(hello_lib  SHARED hello.c)<br>
<br>
③test_dir/top/CMakeLists.txt:<br>
include_directories(../sub_hello)<br>
add_executable(main main.c)<br>
target_link_libraries(main hello_lib)<br>
<br>
④test_dir/sub_hello/hello.c:<br>
#include <stdio.h><br>
void HelloFunc()<br>
{<br>
        printf("###hello###\n");<br>
}<br>
<br>
⑤test_dir/top/main.c:<br>
int main()<br>
{<br>
        HelloFunc();<br>
        return 0;<br>
}<br>
<br>
After i cmake this project, generated .sln and .proj files, then i built it<br>
and i get an error in vs:<br>
<br>
Error   LNK1104 can't open file "..\sub_hello\Debug\hello_lib.lib"<br>
<br>
In folder ..\sub_hello\Debug\ , there was not a hello_lib.lib existed.<br>
Then i look thorough and found a solution:<br>
<br>
Add "set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES)" in file<br>
test_dir/sub_hello/hello.c<br>
<br>
Then i built this solution again, this time it success, and hello_lib.lib<br>
and main.exe was generated. But when i try to run main.exe, an error<br>
occured: "lose hello_lib.dll". And i moved hello_lib.dll into the folder of<br>
main.exe, and it worked well.<br>
<br>
There are two questions i could not figure out:<br>
①Is this "..\sub_hello\Debug\hello_lib.lib" associates with<br>
"..\sub_hello\Debug\hello_lib.dll"? For windows can not use .dll directly,<br>
and use a .lib to record the .dll's  entrance and location.<br>
②How to solve the problem of main.exe can not find .dll file.<br>
<br>
Best regards,<br>
Chao Zhang<br>
<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://cmake.3232098.n2.nabble.com/How-to-link-shared-lib-on-windows-visual-studio-2014-tp7593346.html" rel="noreferrer" target="_blank">http://cmake.3232098.n2.nabble.com/How-to-link-shared-lib-on-windows-visual-studio-2014-tp7593346.html</a><br>
Sent from the CMake mailing list archive at Nabble.com.<br>
--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/mailman/listinfo/cmake</a></blockquote></div>