[CMake] Visual Studio generator running custom_command twice

frodak17 frodak17 at gmail.com
Thu Mar 14 13:30:12 EDT 2019


On Thu, Mar 14, 2019 at 12:53 PM frodak17 <frodak17 at gmail.com> wrote:

>
>
> On Thu, Mar 14, 2019 at 1:13 AM Paul Smith <paul at mad-scientist.net> wrote:
>
>> I have a situation where I've created a custom command to generate .cpp
>> files to be compiled (in my case running bison/flex).
>>
>> I'm using CMake 3.13.4
>>
>>   set(MyParserOutput
>>       ${OUT_DIR}/MyParser.tab.cpp
>>       ${OUT_DIR}/MyParser.tab.hpp)
>>
>>   add_custom_target(MyGenParser DEPENDS ${MyParserOutput})
>>
>> Then I have two different libraries, both depending on this:
>>
>>   add_library(OneLib STATIC ${MyParserOutput} ...)
>>   add_dependencies(OneLib MyGenparser)
>>
>>
>>   add_library(TwoLib STATIC ${MyParserOutput} ...)
>>   add_dependencies(TwoLib MyGenparser)
>>
>>
>> From add_custom_command()
> Do not list the output in more than one independent target that may build
> in parallel or the two instances of the rule may conflict (instead use the
> add_custom_target()
> <https://cmake.org/cmake/help/v3.14/command/add_custom_target.html#command:add_custom_target>
> command to drive the command and make the other targets depend on that one)
>
> Removing ${MyParserOutput} from both add_library() should fix the issue.
>
> Best regards,
> F
>

Sorry, It didn't register at first that about the cpp file that needs to
get compiled into the library.

That makes it a little more complicated.

In that case you need to keep ${MyParserOutput} and set the GENERATED
properties for the files.

Also the building the custom target needs to be done in a separate
directory as the add_custom_commands() need to be in a different
CMakeLists.txt file from the libraries.  Otherwise the rules get pulled
into the libraries and cause the commands to be run multiple times.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190314/ad2debe8/attachment.html>


More information about the CMake mailing list