浏览代码

fix dynamic linking (#714)

Julius Pfrommer 8 年之前
父节点
当前提交
2fa33020b8
共有 3 个文件被更改,包括 26 次插入12 次删除
  1. 8 3
      CMakeLists.txt
  2. 6 2
      doc/building.rst
  3. 12 7
      include/ua_config.h.in

+ 8 - 3
CMakeLists.txt

@@ -80,6 +80,12 @@ option(UA_BUILD_DOCUMENTATION "Generate doxygen/sphinx documentation" OFF)
 option(UA_BUILD_SELFSIGNED_CERTIFICATE "Generate self-signed certificate" OFF)
 mark_as_advanced(UA_BUILD_SELFSIGNED_CERTIFICATE)
 
+# Building shared libs (dll, so)
+set(UA_DYNAMIC_LINKING OFF)
+if(BUILD_SHARED_LIBS)
+  set(UA_DYNAMIC_LINKING ON)
+endif()
+
 #####################
 # Compiler Settings #
 #####################
@@ -336,9 +342,8 @@ endif()
 add_library(open62541 $<TARGET_OBJECTS:open62541-object>)
 target_link_libraries(open62541 ${open62541_LIBRARIES})
 
-target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING)
-target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING)
-# target_compile_definitions(open62541-static PRIVATE -DUA_DYNAMIC_LINKING)
+target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
+target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
 
 if(WIN32)
     target_link_libraries(open62541 ws2_32)

+ 6 - 2
doc/building.rst

@@ -152,8 +152,6 @@ This group contains build options related to the supported OPC UA features.
 **UA_ENABLE_COVERAGE**
    Measure the coverage of unit tests
 
-
-
 Some options are marked as advanced. The advanced options need to be toggled to
 be visible in the cmake GUIs.
 
@@ -170,3 +168,9 @@ be visible in the cmake GUIs.
    Enable stateless extension
 **UA_ENABLE_NONSTANDARD_UDP**
    Enable udp extension
+
+Building a shared library
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+open62541 is small enough that most users will want to statically link the library into their programs. If a shared library (.dll, .so) is required, this can be enabled in CMake with the `BUILD_SHARED_LIBS` option.
+Note that this option modifies the `ua_config.h` file that is also included in `open62541.h` for the single-file distribution.

+ 12 - 7
include/ua_config.h.in

@@ -45,27 +45,32 @@ extern "C" {
 /**
  * Function Export
  * --------------- */
-#ifdef _WIN32
-# ifdef UA_DYNAMIC_LINKING
+/* On Win32: Define UA_DYNAMIC_LINKING and UA_DYNAMIC_LINKING_EXPORT in order to
+   export symbols for a DLL. Define UA_DYNAMIC_LINKING only to import symbols
+   from a DLL.*/
+#cmakedefine UA_DYNAMIC_LINKING
+#if defined(_WIN32) && defined(UA_DYNAMIC_LINKING)
+# ifdef UA_DYNAMIC_LINKING_EXPORT /* export dll */
 #  ifdef __GNUC__
 #   define UA_EXPORT __attribute__ ((dllexport))
 #  else
 #   define UA_EXPORT __declspec(dllexport)
 #  endif
-# else
+# else /* import dll */
 #  ifdef __GNUC__
-#   define UA_EXPORT __attribute__ ((dllexport))
+#   define UA_EXPORT __attribute__ ((dllimport))
 #  else
 #   define UA_EXPORT __declspec(dllimport)
 #  endif
 # endif
-#else
+#else /* non win32 */
 # if __GNUC__ || __clang__
 #  define UA_EXPORT __attribute__ ((visibility ("default")))
-# else
-#  define UA_EXPORT
 # endif
 #endif
+#ifndef UA_EXPORT
+# define UA_EXPORT /* fallback to default */
+#endif
 
 /**
  * Inline Functions