<div class="gmail_quote">On Tue, Mar 29, 2011 at 1:58 AM, Michael Hertling <span dir="ltr"><<a href="mailto:mhertling@online.de" target="_blank">mhertling@online.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div></div><div>On 03/29/2011 07:47 AM, Michael Hertling wrote:<br>
> On 03/28/2011 08:23 PM, David Doria wrote:<br>
>> I have setup a list of definitions:<br>
>><br>
>> SET(MAIN_BUILD_DEFINITIONS "${MAIN_BUILD_DEFINITIONS} UNIX;")<br>
>> SET(MAIN_BUILD_DEFINITIONS "${MAIN_BUILD_DEFINITIONS} PIXEL_DIMENSION=3;")<br>
>><br>
>> I display them and apply them to my executable as follows:<br>
>><br>
>> add_executable(ImageCompleter ${MainSources})<br>
>> message("Main build definitions: " ${MAIN_BUILD_DEFINITIONS})<br>
>> set_target_properties(ImageCompleter PROPERTIES COMPILE_DEFINITIONS<br>
>> "${MAIN_BUILD_DEFINITIONS}")<br>
>><br>
>> The output is:<br>
>> Main build definitions: UNIX PIXEL_DIMENSION=3<br>
>><br>
>> which looks correct (i.e. UNIX was defined)<br>
>><br>
>> However, in my code there is:<br>
>><br>
>> #if defined(UNIX)<br>
>> ... some code...<br>
>> #else<br>
>> #error "Not implemented for this platform!"<br>
>> #endif<br>
>><br>
>> When I build, the error is produced, indicating that UNIX was not defined.<br>
>><br>
>> I created a small standalone example and it worked as expected... any<br>
>> suggestions of what else to check?<br>
>><br>
>> Thanks,<br>
>><br>
>> David<br>
><br>
> AFAICS, you mess up the value of the MAIN_BUILD_DEFINITIONS<br>
> list variable. Look at the following CMakeLists.txt file:<br>
><br>
> CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)<br>
> PROJECT(COMPDEFS C)<br>
> FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")<br>
> ADD_EXECUTABLE(main main.c)<br>
> SET(MAIN_BUILD_DEFINITIONS<br>
> "${MAIN_BUILD_DEFINITIONS} UNIX;")<br>
> LIST(LENGTH MAIN_BUILD_DEFINITIONS n)<br>
> MESSAGE("MAIN_BUILD_DEFINITIONS: ${MAIN_BUILD_DEFINITIONS} -- n=${n}")<br>
> SET(MAIN_BUILD_DEFINITIONS<br>
> "${MAIN_BUILD_DEFINITIONS} PIXEL_DIMENSION=3;")<br>
> LIST(LENGTH MAIN_BUILD_DEFINITIONS n)<br>
> MESSAGE("MAIN_BUILD_DEFINITIONS: ${MAIN_BUILD_DEFINITIONS} -- n=${n}")<br>
> SET_TARGET_PROPERTIES(main PROPERTIES<br>
> COMPILE_DEFINITIONS "${MAIN_BUILD_DEFINITIONS}")<br>
><br>
> CMake's output contains:<br>
><br>
> MAIN_BUILD_DEFINITIONS: UNIX; -- n=2<br>
> MAIN_BUILD_DEFINITIONS: UNIX; PIXEL_DIMENSION=3; -- n=3<br>
><br>
> Provided that MAIN_BUILD_DEFINITIONS is initially empty, the command<br>
> SET(MAIN_BUILD_DEFINITIONS "${MAIN_BUILD_DEFINITIONS} UNIX;") makes<br>
> MAIN_BUILD_DEFINITIONS a list of *two* elements: " UNIX" - note the<br>
> leading blank - and the empty string "", and the second SET() adds<br>
> "PIXEL_DIMENSION=3". Probably, this is not what you intended. ;)<br>
><br>
> Instead, try<br>
><br>
> LIST(APPEND MAIN_BUILD_DEFINITIONS "UNIX")<br>
> LIST(APPEND MAIN_BUILD_DEFINITIONS "PIXEL_DIMENSION=3")<br>
><br>
> and CMake's output changes to<br>
><br>
> MAIN_BUILD_DEFINITIONS: UNIX -- n=1<br>
> MAIN_BUILD_DEFINITIONS: UNIX;PIXEL_DIMENSION=3 -- n=2<br>
><br>
> which should work when used as COMPILE_DEFINITIONS property.<br>
><br>
> IMO, the rule of thumb is: Don't use semicolons explicitly in SET()<br>
> commands to assign list variables, but let CMake do this by itself.<br>
> Moreover, be particularly careful when SETting list values, e.g.<br>
><br>
> SET(A1 "a")<br>
> SET(AB1 "${A1} b")<br>
> LIST(LENGTH AB1 n)<br>
> MESSAGE("AB1: ${AB1} -- n=${n}")<br>
> SET(A2 "a" "a")<br>
> SET(AB2 "${A2} b")<br>
> LIST(LENGTH AB2 n)<br>
> MESSAGE("AB2: ${AB2} -- n=${n}")<br>
><br>
> results in<br>
><br>
> AB1: a b -- n=1<br>
> AB2: a;a b -- n=2<br>
><br>
> i.e. AB1 is a non-list value which might by unexpected, and AB2 remains<br>
> a list with just two elements which might be unexpected, too. Here, the<br>
> correct coding is SET(AB1 "${A1}" "b") and SET(AB2 "${A2}" "b") which<br>
> could nevertheless lead to AB{1,2} being lists of length two with an<br>
> empty first element if AB{1,2} have been empty before. When adding<br>
<br>
</div></div>Oops: "... if A{1,2} have been empty before.", to be exact. 8-0<br>
<div><div></div><div><br>
> elements to lists it's best to use the LIST(APPEND ...) command.<br>
><br>
> BTW, I can see the abovementioned CMakeLists.txt work correctly with<br>
> CMake 2.8.4, so what version of CMake do you use for your project?<br>
><br>
> Regards,<br>
><br>
> Michael<br>
</div></div><div><br></div></blockquote><div class="gmail_quote"><br></div>Hm, I see what you're saying regarding set() vs list(append), however I don't think that is the problem here. I tried this:</div><div class="gmail_quote">
<br></div><div class="gmail_quote"><div class="gmail_quote">list(APPEND my_definitions "UNIX")</div><div class="gmail_quote">list(APPEND my_definitions "USE_ITK")</div><div class="gmail_quote">list(APPEND my_definitions "USE_FLOAT_PIXELS")</div>
<div class="gmail_quote">list(APPEND my_definitions "PIXEL_DIMENSION=1")</div><div class="gmail_quote">message("my definitions: ${my_definitions}")</div><div class="gmail_quote">set_target_properties(ImageCompleter1f PROPERTIES COMPILE_DEFINITIONS "${my_definitions}")</div>
<div class="gmail_quote"><br></div><div class="gmail_quote">(with and without quotes around ${my_definitions} in the set_target_properties line) </div><div class="gmail_quote"><br></div><div class="gmail_quote">The message command seems to be fine:</div>
<div class="gmail_quote"><div class="gmail_quote">my definitions: UNIX;USE_ITK;USE_FLOAT_PIXELS;PIXEL_DIMENSION=1</div><div class="gmail_quote"><br></div><div class="gmail_quote">but the #if defined(UNIX) still fails in the code!</div>
<div><br></div></div></div><div class="gmail_quote"><div>David </div></div>