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

2016-04-26 Thread Chaos Zhang
Thanks again Mr. Atkins.

Chuck Atkins wrote
> May others have mentioned the various different ways to address generating
> import libraries.  One thing that will help with the "can't find .dll"
> issue is to change the default location that various components end up.
> With Windows shared libraries, you have both a runtime component (.dll)
> and
> a link time component (.lib).  Setting the following at the top of your
> CMakeLists.txt:
> 
> if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
>   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
> endif()
> if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
>   set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
> endif()
> if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
>   set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
> endif()
> 
> This is a common pattern to place near the beginning of a top-level
> CMakeLists.txt after the project(...) copmmand.  What this does is make
> sure that all runtime components end up in the bin directory, including
> both .exe's and .dll's, and that all link-time components, including .so's
> and .a's in Linux and .lib's on Windows, end up in the the lib directory.
> This way when you run your executable on Windows, their necessary runtime
> components live next to them.  It also consolidates all your build
> products
> into one place instead of leaving them scattered across the various
> sub-directories of the build folder.
> 
> - Chuck
> 
> On Tue, Apr 26, 2016 at 9:22 AM, Chaos Zhang 

> zcsd2012@

>  wrote:
> 
>> Hi, all,
>>
>> Thanks for your sincerely help, and i understand it clearly now. ( ^_^ )
>>
>> Best regards,
>> Chao
>>
>>
>> J. Caleb Wherry wrote
>> > (Shameless plug ahead)
>> >
>> > If you want to see a working example of what I said above, you can
>> check
>> > out my ExampleCMakeProject:
>> >
>> > https://github.com/calebwherry/example-cmake-project
>> >
>> > It is currently being built and tested on TravisCI (Linux + Mac [GGC5
>> and
>> > Clang3.7]) and Appveyor (Windows [VS2015]) and provides a simple
>> example
>> > of
>> > how to write/structure CMake for cross-platform C/C++ projects. You can
>> > ignore the python wrapper if you want, it just makes working with the
>> > CMake
>> > exe and system builds easier (calculates core counts, creates
>> directories
>> > for build, nicer CLI options, etc) since I find the options to CMake to
>> be
>> > very... verbose.
>> >
>> > The POST_BUILD example is here:
>> >
>> >
>> https://github.com/calebwherry/example-cmake-project/blob/master/src/app/test-app/CMakeLists.txt
>> >
>> > If you really are looking to get a one-off project working, Decker's
>> > recommendation about PATH modification is easiest. It's just not
>> something
>> > I think is viable in the long run and doesn't work as an actual
>> solution
>> > to
>> > the problem if you are trying to solve it for many projects.
>> >
>> > -Caleb
>> >
>> > On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang 
>>
>> > zcsd2012@
>>
>> >  wrote:
>> >
>> >> 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
>> > 
> 
>> >> 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
>> >> 

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

2016-04-26 Thread Chuck Atkins
May others have mentioned the various different ways to address generating
import libraries.  One thing that will help with the "can't find .dll"
issue is to change the default location that various components end up.
With Windows shared libraries, you have both a runtime component (.dll) and
a link time component (.lib).  Setting the following at the top of your
CMakeLists.txt:

if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
endif()
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
endif()
if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
endif()

This is a common pattern to place near the beginning of a top-level
CMakeLists.txt after the project(...) copmmand.  What this does is make
sure that all runtime components end up in the bin directory, including
both .exe's and .dll's, and that all link-time components, including .so's
and .a's in Linux and .lib's on Windows, end up in the the lib directory.
This way when you run your executable on Windows, their necessary runtime
components live next to them.  It also consolidates all your build products
into one place instead of leaving them scattered across the various
sub-directories of the build folder.

- Chuck

On Tue, Apr 26, 2016 at 9:22 AM, Chaos Zhang  wrote:

> Hi, all,
>
> Thanks for your sincerely help, and i understand it clearly now. ( ^_^ )
>
> Best regards,
> Chao
>
>
> J. Caleb Wherry wrote
> > (Shameless plug ahead)
> >
> > If you want to see a working example of what I said above, you can check
> > out my ExampleCMakeProject:
> >
> > https://github.com/calebwherry/example-cmake-project
> >
> > It is currently being built and tested on TravisCI (Linux + Mac [GGC5 and
> > Clang3.7]) and Appveyor (Windows [VS2015]) and provides a simple example
> > of
> > how to write/structure CMake for cross-platform C/C++ projects. You can
> > ignore the python wrapper if you want, it just makes working with the
> > CMake
> > exe and system builds easier (calculates core counts, creates directories
> > for build, nicer CLI options, etc) since I find the options to CMake to
> be
> > very... verbose.
> >
> > The POST_BUILD example is here:
> >
> >
> https://github.com/calebwherry/example-cmake-project/blob/master/src/app/test-app/CMakeLists.txt
> >
> > If you really are looking to get a one-off project working, Decker's
> > recommendation about PATH modification is easiest. It's just not
> something
> > I think is viable in the long run and doesn't work as an actual solution
> > to
> > the problem if you are trying to solve it for many projects.
> >
> > -Caleb
> >
> > On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang 
>
> > zcsd2012@
>
> >  wrote:
> >
> >> 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
> > 
> >> 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 

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

2016-04-26 Thread Chaos Zhang
Hi, all,

Thanks for your sincerely help, and i understand it clearly now. ( ^_^ )

Best regards,
Chao


J. Caleb Wherry wrote
> (Shameless plug ahead)
> 
> If you want to see a working example of what I said above, you can check
> out my ExampleCMakeProject:
> 
> https://github.com/calebwherry/example-cmake-project
> 
> It is currently being built and tested on TravisCI (Linux + Mac [GGC5 and
> Clang3.7]) and Appveyor (Windows [VS2015]) and provides a simple example
> of
> how to write/structure CMake for cross-platform C/C++ projects. You can
> ignore the python wrapper if you want, it just makes working with the
> CMake
> exe and system builds easier (calculates core counts, creates directories
> for build, nicer CLI options, etc) since I find the options to CMake to be
> very... verbose.
> 
> The POST_BUILD example is here:
> 
> https://github.com/calebwherry/example-cmake-project/blob/master/src/app/test-app/CMakeLists.txt
> 
> If you really are looking to get a one-off project working, Decker's
> recommendation about PATH modification is easiest. It's just not something
> I think is viable in the long run and doesn't work as an actual solution
> to
> the problem if you are trying to solve it for many projects.
> 
> -Caleb
> 
> On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang 

> zcsd2012@

>  wrote:
> 
>> 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 
> 
>> 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.
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
> 
> 
> 
> 
> -- 
> J. Caleb Wherry
> *Scientific Software Engineer*
> 
> http://www.calebwherry.com;
> http://www.calebwherry.com
> +1 (615) 708-5651

> calebwherry@

> 
> -- 
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> 

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

2016-04-25 Thread J. Caleb Wherry
(Shameless plug ahead)

If you want to see a working example of what I said above, you can check
out my ExampleCMakeProject:

https://github.com/calebwherry/example-cmake-project

It is currently being built and tested on TravisCI (Linux + Mac [GGC5 and
Clang3.7]) and Appveyor (Windows [VS2015]) and provides a simple example of
how to write/structure CMake for cross-platform C/C++ projects. You can
ignore the python wrapper if you want, it just makes working with the CMake
exe and system builds easier (calculates core counts, creates directories
for build, nicer CLI options, etc) since I find the options to CMake to be
very... verbose.

The POST_BUILD example is here:

https://github.com/calebwherry/example-cmake-project/blob/master/src/app/test-app/CMakeLists.txt

If you really are looking to get a one-off project working, Decker's
recommendation about PATH modification is easiest. It's just not something
I think is viable in the long run and doesn't work as an actual solution to
the problem if you are trying to solve it for many projects.

-Caleb

On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang  wrote:

> 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 
> 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.
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake




-- 
J. Caleb Wherry
*Scientific Software Engineer*


http://www.calebwherry.com
+1 (615) 708-5651
calebwhe...@gmail.com
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

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

2016-04-25 Thread J. Caleb Wherry
Altering the path, updating the registry, or having a custom install step
that installs into a build location are all options that aren't scalable
since they apply specifically to the one project. Paths would get huge if
this was to be done in practice for multiple projects.

Also, the OP seems to building from VS so being able to press F5 and
everything just work is nice. Adding a POST_BULLD step that copies the DLL
to the build location of the exe is the best way to solve the problem.

Lastly, adding an install step just muddles what "install" means, IMO. You
aren't really installing anything, you are just building a project and you
want it to run after building. Installing brings a lot more baggage and
using it here seems like a bad idea.

-Caleb

On Monday, April 25, 2016, J Decker  wrote:

>
> On Apr 25, 2016 6:41 PM, "J. Caleb Wherry"  > wrote:
> >
> > (1)
> >
> > You typically want to define the entry point in the source itself. You
> can use the 'CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS' in CMake but you usually do
> something like this in the source:
> >
> > #ifdef WIN32
> > #define PUBLIC_API __declspec(dllexport)
> > #else
> > #define PUBLIC_API
> > #endif
> >
> > PUBLIC_API void HelloFunc();
> >
> > That way when you are building a shared lib on windows it will export
> those symbols which in turn creates the .lib. Without the exported symbols,
> you will not have an import lib.
> >
> > (2)
> >
> > The missing DLL is a pain that you are just going to have to deal with
> unless you want to do a bunch of voodoo with the registry or copy the DLLs.
> Typically, there are POST_BUILD steps that copy the needed DLLs into the
> same directory as the exe. There are many stackoverflow questions dealing
> with this, first one googled here:
> >
>
> Could just set the path environment variable... No registry hack.. Or
> include install rules and just build install before running.
> >
> http://stackoverflow.com/questions/5765986/how-can-i-run-an-exe-with-dlls-in-separate-directory
> >
> > -Caleb
> >
> >
> > On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang  > wrote:
> >>
> >> 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 
> >> 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.
> >> --
> >>
> >> Powered by www.kitware.com
> >>
> >> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> >>
> >> Kitware offers various services to support the CMake community. For
> more information on each offering, please visit:
> >>
> >> CMake Support: http://cmake.org/cmake/help/support.html
> >> CMake Consulting: 

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

2016-04-25 Thread J Decker
On Apr 25, 2016 6:41 PM, "J. Caleb Wherry"  wrote:
>
> (1)
>
> You typically want to define the entry point in the source itself. You
can use the 'CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS' in CMake but you usually do
something like this in the source:
>
> #ifdef WIN32
> #define PUBLIC_API __declspec(dllexport)
> #else
> #define PUBLIC_API
> #endif
>
> PUBLIC_API void HelloFunc();
>
> That way when you are building a shared lib on windows it will export
those symbols which in turn creates the .lib. Without the exported symbols,
you will not have an import lib.
>
> (2)
>
> The missing DLL is a pain that you are just going to have to deal with
unless you want to do a bunch of voodoo with the registry or copy the DLLs.
Typically, there are POST_BUILD steps that copy the needed DLLs into the
same directory as the exe. There are many stackoverflow questions dealing
with this, first one googled here:
>

Could just set the path environment variable... No registry hack.. Or
include install rules and just build install before running.
>
http://stackoverflow.com/questions/5765986/how-can-i-run-an-exe-with-dlls-in-separate-directory
>
> -Caleb
>
>
> On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang  wrote:
>>
>> 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 
>> 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.
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
>
>
>
>
> --
> J. Caleb Wherry
> Scientific Software Engineer
>
> http://www.calebwherry.com
> +1 (615) 708-5651
> calebwhe...@gmail.com
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware 

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

2016-04-25 Thread Craig Scott
Also see the GenerateExportHeader module
 which
can handle much of this for you in a more platform independent way. The
examples in the docs talk about classes, but it can also be applied to
functions.


On Tue, Apr 26, 2016 at 11:33 AM, J Decker  wrote:

> Add __declspec(dllexport) before void HelloFunc()
> On Apr 25, 2016 6:01 PM, "Chaos Zhang"  wrote:
>
>> 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 
>> 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.
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>



-- 
Craig Scott
Melbourne, Australia
http://crascit.com
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

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

2016-04-25 Thread J. Caleb Wherry
(1)

You typically want to define the entry point in the source itself. You can
use the 'CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS' in CMake but you usually do
something like this in the source:

#ifdef WIN32
#define PUBLIC_API __declspec(dllexport)
#else
#define PUBLIC_API
#endif

PUBLIC_API void HelloFunc();

That way when you are building a shared lib on windows it will export those
symbols which in turn creates the .lib. Without the exported symbols, you
will not have an import lib.

(2)

The missing DLL is a pain that you are just going to have to deal with
unless you want to do a bunch of voodoo with the registry or copy the DLLs.
Typically, there are POST_BUILD steps that copy the needed DLLs into the
same directory as the exe. There are many stackoverflow questions dealing
with this, first one googled here:

http://stackoverflow.com/questions/5765986/how-can-i-run-an-exe-with-dlls-in-separate-directory

-Caleb


On Mon, Apr 25, 2016 at 9:01 PM, Chaos Zhang  wrote:

> 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 
> 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.
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake




-- 
J. Caleb Wherry
*Scientific Software Engineer*


http://www.calebwherry.com
+1 (615) 708-5651
calebwhe...@gmail.com
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

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

2016-04-25 Thread J Decker
Add __declspec(dllexport) before void HelloFunc()
On Apr 25, 2016 6:01 PM, "Chaos Zhang"  wrote:

> 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 
> 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.
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

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

2016-04-25 Thread Chaos Zhang
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 
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.
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake