cmake - two targets that depends on single static library that should be compiled based on the target that is being built

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

cmake - two targets that depends on single static library that should be compiled based on the target that is being built

ugesh reddy
Hello,

I couldn't find any solution's for this problem even after posting the question on Reddit/stack overflow, so I am posting it here.

My question is as follows:

I have a static library and two target executables, let's call them **libA**, **EXE1**, **EXE2**.

**libA** has pre-processor macros which needs to be enabled or disabled and another static library which needs to be linked or ignored based on the target executable that I am building.

Let's say, if I am building EXE1. Then I need to enable the macros in libA and link another static library to it.

If I am building EXE2, I need to disabled the macros in libA and don't link to another library.

I don't want to create two targets with same source files.

I am confused on how to solve this issue. Please kindly help in solving this issue.


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: cmake - two targets that depends on single static library that should be compiled based on the target that is being built

Kyle Edwards
On Tue, 2019-07-02 at 14:18 +0200, ugesh reddy wrote:

> Hello,
>
> I couldn't find any solution's for this problem even after posting
> the question on Reddit/stack overflow, so I am posting it here.
>
> My question is as follows:
>
> I have a static library and two target executables, let's call them
> **libA**, **EXE1**, **EXE2**.
>
> **libA** has pre-processor macros which needs to be enabled or
> disabled and another static library which needs to be linked or
> ignored based on the target executable that I am building.
>
> Let's say, if I am building EXE1. Then I need to enable the macros in
> libA and link another static library to it.
>
> If I am building EXE2, I need to disabled the macros in libA and
> don't link to another library.
>
> I don't want to create two targets with same source files.

I'm sorry to say it, but creating two targets is actually exactly what
you want to do here. If your concern is code duplication, you can make
the code much shorter by using variables. Example:

set(libA_SRCS
  src1.c
  src2.c
  src3.c
  # lots of other sources...
  )
add_library(libA1 STATIC ${libA_SRCS})
target_link_libraries(libA1 otherlib)
add_library(libA2 STATIC ${libA_SRCS})

Kyle
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: cmake - two targets that depends on single static library that should be compiled based on the target that is being built

ugesh reddy
Ok, seems there is no other way then. Thank you for the clarification. 

On Tue, Jul 2, 2019 at 3:44 PM Kyle Edwards <[hidden email]> wrote:
On Tue, 2019-07-02 at 14:18 +0200, ugesh reddy wrote:
> Hello,
>
> I couldn't find any solution's for this problem even after posting
> the question on Reddit/stack overflow, so I am posting it here.
>
> My question is as follows:
>
> I have a static library and two target executables, let's call them
> **libA**, **EXE1**, **EXE2**.
>
> **libA** has pre-processor macros which needs to be enabled or
> disabled and another static library which needs to be linked or
> ignored based on the target executable that I am building.
>
> Let's say, if I am building EXE1. Then I need to enable the macros in
> libA and link another static library to it.
>
> If I am building EXE2, I need to disabled the macros in libA and
> don't link to another library.
>
> I don't want to create two targets with same source files.

I'm sorry to say it, but creating two targets is actually exactly what
you want to do here. If your concern is code duplication, you can make
the code much shorter by using variables. Example:

set(libA_SRCS
  src1.c
  src2.c
  src3.c
  # lots of other sources...
  )
add_library(libA1 STATIC ${libA_SRCS})
target_link_libraries(libA1 otherlib)
add_library(libA2 STATIC ${libA_SRCS})

Kyle


--
 Ugesh

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: cmake - two targets that depends on single static library that should be compiled based on the target that is being built

Marc Herbert
In reply to this post by ugesh reddy

I have a static library and two target executables, let's call them **libA**, **EXE1**, **EXE2**.

If I am building EXE2, I need to disabled the macros in libA and don't link to another library.
[...]
I don't want to create two targets with same source files.

I'm curious why you wanted two executable targets but not the corresponding two library targets, I don't think you said why. I would have thought you'd want A. either to be able to build both configurations at the same time, B. or one project configuration at a time. So why want A for executables but B for libraries? 
 

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake