Hi Chao,

You want to let CMake to as much of the work for you as possible.  You're
still trying to explicitly pass the path to the library file to
target_link_libraries.  If you look at the line:

target_link_library(exe1 "-Wl, --whole-archive ../sub_dir1/liblib1.a
--no-whole-archive")

there's no CMake target name so CMake can't know about the dependency.
However, if you use the target name instead:

top_dir/CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(foo)
add_subdirectory(sub_dir2)
add_subdirectory(sub_dir1)

top_dir/sub_dir1/CMakeLists.txt
add_library(L1 1.c)

top_dit/sub_dir2/CMakeLists.txt
add_executable(exe1 main.c)
target_link_libraries(exe1 "-Wl,--whole-archive" *L1*
"-Wl,--no-whole-archive")

Notice how in the target_link_libraries line for exe1, the link options are
separate instead of 1 big string combining them all and the actual CMake
target name, L1, is used instead of the output file libL1.a.  You can see
the resulting output is what you want:

$ make VERBOSE=1
...
[100%] Linking C executable exe1
...
/usr/bin/cc     CMakeFiles/exe1.dir/main.c.o  -o exe1 -rdynamic
*-Wl,--whole-archive
../sub_dir1/libL1.a -Wl,--no-whole-archive*

Generally speaking, always use target names in a CMakeLists.txt instead of
the actual output file.  There are a few situations where you may need the
actual file name but they are uncommon and even then you would do it
through target properties and generator expressions rather than hard code
the library file name.

- Chuck

On Mon, May 30, 2016 at 8:35 AM, Chaos Zhang <zcsd2...@gmail.com> wrote:

> Hi, all,
>
> Thanks for taking your time to review my email. I have a demo project and
> it's structure like as below:
>
> top_dir
>     CMakeLists.txt
>     sub_dir1
>         CMakeLists.txt
>     sub_dir2
>         CMakeLists.txt
>
> top_dir/sub_dir1/CMakeLists.txt used to build `lib1` by using
> `add_library(lib1 ...)`,
> top_dir/sub_dir2/CMakeLists.txt used to build `exe1` with linking lib1 by
> `target_link_library(exe1 lib1)`.
> And the content of top_dir/CMakeLists.txt is as below:
>
> add_subdirectory(sub_dir2)
> add_subdirectory(sub_dir1)
>
> Normally, when build target exe1, cmake will check dependency so `lib1`
> will
> be built before building exe1. The problem is I am transfering an existed
> makefile project into CMake, and there are many gcc link options, like
> "whole-archive ... no-whole-archive, allow-mutiple-definition", if use like
> `target_link_library(exe1 "-Wl, --whole-archive ../sub_dir1/liblib1.a
> --no-whole-archive")`(The form like this, and this may not work, it just a
> e.g.), cmake seem don't built `lib1` any more. Is there any way i can use
> target_link_library like `target_link_library(exe1 "-Wl, --whole-archive
> ../sub_dir1/liblib1.a")` and cmake link dependency checking still work, or
> other way i can transfer these gcc link options into cmake?
>
> Thanks a lot,
> Chao
>
>
>
> --
> View this message in context:
> http://cmake.3232098.n2.nabble.com/How-to-hundle-gcc-link-options-like-whole-archive-allow-multiple-definition-in-CMake-tp7593563.html
> Sent from the CMake mailing list archive at Nabble.com.
> --
>
> 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:
> http://public.kitware.com/mailman/listinfo/cmake
>
-- 

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:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to