[CMake] Using gcc to link a C++ library on SunOS

Pasi Valminen pasi.valminen at gmail.com
Wed Jul 6 23:44:28 EDT 2011


Hi,

I noticed gcc is used to link a c++ library on SunOS due to Sun shipping GCC
without a shared libstdc++ in the past (see discussion
http://www.cmake.org/pipermail/cmake/2006-August/010535.html here). Since
this does not seem to be the case anymore, changing the
CMAKE_CXX_CREATE_SHARED_LIBRARY rule on new projects in vain becomes a
nuisance as using gcc to invoke the linker has problems; missing options g++
tells the linker e.g. when profiling and maybe static initialization issues
as well. So, could we check if libstdc++.so is present and if so, use the
default rule from CMakeDefaultMakeRuleVariables.cmake file instead. I made a
small patch to Modules/Platform/SunOS.cmake that does exatcly that:

--- SunOS.cmake.orig    2011-07-07 11:21:22.331321841 +0800
+++ SunOS.cmake    2011-07-07 11:21:27.149332933 +0800
@@ -5,12 +5,20 @@
    SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")
 ENDIF(CMAKE_SYSTEM MATCHES "SunOS-4.*")

+# Take the default c++ shared library creation rule from the
+# CMakeDefaultMakeRuleVariables.cmake file unless using GCC and
libstdc++.so
+# does not exist, in which case fall back to the old implementation;
+# using gcc to invoke the linker.
 IF(CMAKE_COMPILER_IS_GNUCXX)
   IF(CMAKE_COMPILER_IS_GNUCC)
-    SET(CMAKE_CXX_CREATE_SHARED_LIBRARY
-        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS>
<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>
<CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS>
<LINK_LIBRARIES>")
-  ELSE(CMAKE_COMPILER_IS_GNUCC)
-    # Take default rule from CMakeDefaultMakeRuleVariables.cmake.
+    EXECUTE_PROCESS(
+      COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libstdc++.so
+      OUTPUT_VARIABLE SHARED_LIBSTDCXX_FILENAME
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+    IF(NOT EXISTS "${SHARED_LIBSTDCXX_FILENAME}")
+      SET(CMAKE_CXX_CREATE_SHARED_LIBRARY
+          "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS>
<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>
<CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS>
<LINK_LIBRARIES>")
+    ENDIF(NOT EXISTS "${SHARED_LIBSTDCXX_FILENAME}")
   ENDIF(CMAKE_COMPILER_IS_GNUCC)
 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
 INCLUDE(Platform/UnixPaths)

Cheers,
Pasi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110707/eab4e3fa/attachment.htm>


More information about the CMake mailing list