[CMake] can't get compile_definitions working quite right

Philip Lowman philip at yhbt.com
Fri Sep 5 08:21:27 EDT 2008


On Thu, Sep 4, 2008 at 8:24 PM, Christopher Harvey
<chris at basementcode.com>wrote:

> Hello list,
>
> I'll just start off by pasting my CMakeLists.txt, and I'll explain the
> problem right after.
>
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> ADD_LIBRARY(Portal SHARED
>                         BSPWorld.cc
>             Polygon3D.cc)
> SET_TARGET_PROPERTIES(Portal PROPERTIES
>                COMPILE_FLAGS "-fvisibility=hidden -g"
> #                            COMPILE_FLAGS "-O3"
>                LIBRARY_OUTPUT_DIRECTORY ../bin)
>
> SET_DIRECTORY_PROPERTIES(PROPERTIES
>                                  COMPILE_DEFINITIONS
> "DLLEXPORT=__attribute__ ((visibility("default")))"
>                  #DLLLOCAL="__attribute__ ((visibility(\"hidden\")))")
> )
>
> Basically, I'd like to have DLLEXPORT in the source code replaced with
> __attribute__ ((visibility("default")))
> when looking at the verbose output of the unix makefile I see this in the
> compile command:
>
> /usr/bin/c++   -D"DLLEXPORT=__attribute__ ((visibility("


Don't you need to escape the quotes around "default"?  So:

COMPILE_DEFINITIONS "DLLEXPORT=__attribute__ ((visibility(\"default\")))"


> And the grand goal is to avoid putting this in my code:
>
> #ifdef _MSC_VER
>  #ifdef BUILDING_DLL
>   #define DLLEXPORT __declspec(dllexport)
>  #else
>   #define DLLEXPORT __declspec(dllimport)
>  #endif
>  #define DLLLOCAL
> #else
>  #ifdef HAVE_GCCVISIBILITYPATCH
>   #define DLLEXPORT __attribute__ ((visibility("default")))
>   #define DLLLOCAL __attribute__ ((visibility("hidden")))
>  #else
>   #define DLLEXPORT
>   #define DLLLOCAL
>  #endif
>

If you want your code to be able to be #included by others it's probably
best to leave the defines in a common file (primarily due to the
__declspec(dllimport) declaration).  Ultimately you could force the user to
define what DLLEXPORT and DLLLOCAL means for every compiler they want to use
in their build system but having an include file that does this for you and
them may be a bit more convenient.

Also, is HAVE_GCCVISIBILITYPATCH a custom declaration?  If so you may be
able to rely on __GNUC__ and __GNUC_MINOR__ which could alleviate the need
to define this.

http://predef.sourceforge.net/precomp.html#sec13

HTH

-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20080905/76cac752/attachment.htm>


More information about the CMake mailing list