[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cmake
Subject:    Re: [CMake] Compile shared library and call it's functions
From:       Jakob van Bethlehem <jsvanbethlehem () gmail ! com>
Date:       2016-03-08 15:23:20
Message-ID: CAL1KmUAWi91-r2K=QGHLybjYaWn+9djQ9O+FFY8L3BjY4eCO1A () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hej,

A short side-question - you mentioned you are compiling a DLL, yet you
mention also you are on OSX. I'd expect a .dylib on OSX?
Anywaysz, I think Andreas already answered your question quite clearly: you
probably misinterpret the output of nm, since your CMakeLists.txt file
looks exactly as it should.

Sincerely,
Jakob



On Mon, Mar 7, 2016 at 9:29 PM, Ivan <ivan@sleepyiora.pw> wrote:

> Hello!
> I hope someone could finally help me. I spent about two days to find
> solution for my problem, but with no luck.
> 
> Here is my problem:
> 
> I want to create a shared library (dll on Windows, .so on Linux, .dylib on
> OS X) that contains some functions. This library should export only these
> functions and nothing else. I guarantee that these function are pure C
> functions, so no C++ STL is needed to call them.
> 
> Then I want to create an application that call functions from the shared
> library mentioned above. I mean that the application should not have these
> functions inside. I want OS linker to link the application with the library
> at runtime, when it initializes the application. I know, how to do this on
> Windows with Visual Studio: create DLL project, write functions and
> compile, then there will be two files: .dll and .lib, link the .lib file
> with the application and at runtime Windows will find the .dll. But how can
> I do so with CMake?
> 
> Here is my CMakeLists.txt:
> 
> cmake_minimum_required(VERSION 3.3)
> project(untitled19)
> 
> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
> 
> set(LIBRARY_SRC library.cpp)
> 
> add_library(libra SHARED ${LIBRARY_SRC})
> 
> set(SOURCE_FILES main.cpp)
> add_executable(untitled19 ${SOURCE_FILES})
> target_link_libraries(untitled19 libra)
> 
> 
> This works and both library and executable are compiled. Unfortunately library \
> seems to be linked into the executable: ‘nm' command shows that the executable \
> itself exports needed functions! 
> I think there should be a solution for this, but I cannot find it. Can anyone help \
> me? 
> For more clarification: I'm using OS X El Capitan.
> 
> ——
> 
> Best regards, Ivan.
> 
> 
> 
> 
> --
> 
> 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
> 


[Attachment #5 (text/html)]

<div dir="ltr">Hej,<div><br></div><div>A short side-question - you mentioned you are \
compiling a DLL, yet you mention also you are on OSX. I&#39;d expect a .dylib on \
OSX?</div><div>Anywaysz, I think Andreas already answered your question quite \
clearly: you probably misinterpret the output of nm, since your CMakeLists.txt file \
looks exactly as it should.</div><div><br></div><div>Sincerely,</div><div>Jakob</div><div><br></div><div><br></div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 7, 2016 at 9:29 PM, Ivan \
<span dir="ltr">&lt;<a href="mailto:ivan@sleepyiora.pw" \
target="_blank">ivan@sleepyiora.pw</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div style="word-wrap:break-word">Hello!<div>I hope someone \
could finally help me. I spent about two days to find solution for my problem, but \
with no luck.</div><div><br></div><div>Here is my problem:</div><div><br></div><div>I \
want to create a shared library (dll on Windows, .so on Linux, .dylib on OS X) that \
contains some functions. This library should export only these functions and nothing \
else. I guarantee that these function are pure C functions, so no C++ STL is needed \
to call them.</div><div><br></div><div>Then I want to create an application that call \
functions from the shared library mentioned above. I mean that the application should \
not have these functions inside. I want OS linker to link the application with the \
library at runtime, when it initializes the application. I know, how to do this on \
Windows with Visual Studio: create DLL project, write functions and compile, then \
there will be two files: .dll and .lib, link the .lib file with the application and \
at runtime Windows will find the .dll. But how can I do so with \
CMake?</div><div><br></div><div>Here is my \
CMakeLists.txt:</div><div><br></div><div><pre \
style="font-family:Menlo">cmake_minimum_required(VERSION 3.3) project(untitled19)

set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -std=c++11&quot;)

set(LIBRARY_SRC library.cpp)

add_library(libra SHARED ${LIBRARY_SRC})

set(SOURCE_FILES main.cpp)
add_executable(untitled19 ${SOURCE_FILES})
target_link_libraries(untitled19 libra)</pre><pre \
style="font-family:Menlo"><br></pre><pre style="font-family:Menlo">This works and \
both library and executable are compiled. Unfortunately library seems to be linked \
into the executable: ‘nm' command shows that the executable itself exports needed \
functions!</pre><pre style="font-family:Menlo">I think there should be a solution for \
this, but I cannot find it. Can anyone help me?</pre><pre \
style="font-family:Menlo">For more clarification: I'm using OS X El \
Capitan.</pre><pre style="font-family:Menlo">——</pre><pre \
style="font-family:Menlo">Best regards, Ivan.</pre><pre \
style="font-family:Menlo"><br></pre><pre \
style="font-family:Menlo"><br></pre></div></div><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><br></blockquote></div><br></div>




-- 

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



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic