How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

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

How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chaos Zhang
Hi, all,

I try to use some gcc link option and libs(except libs link by target_link_libraries(...)), i researched and try to use 'CMAKE_EXE_LINKER_FLAGS' in a simple project, as below:
in dir, there are 5 files:

-hello.h
-hello.c
-main.c
-hello.o
-libhello.a

hello.o was compiled by hello.c(gcc -c hello.c) and libhello.a contained hello.o. And other 3 files content:
1.In hello.h:

void hello();

2.In hello.c:

#include<stdio.h>
void hello()
{
    printf("hello\n");
}

3.In main.c:

#include "hello.h"
void main()
{
   hello();
}

When i used gcc -o main main.c libhello.a, the exe file: main generated, and it work well.
Then i write a CMakeLists.txt, the content as below:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
add_executable(main main.c)

When i cmake&make this project, an error occurred: main.c:undefined reference to 'hello'.

Since i wil add a complicated link option and would not conflict with  libs link by target_link_libraries(...), i should figure out how to use 'CMAKE_EXE_LINKER_FLAGS' or other ways, could you please give me some advices? :-)

Thanks,
Chao
Reply | Threaded
Open this post in threaded view
|

Re: How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chuck Atkins
Is there a reason to not use target_link_libraries here? For the purposes of your example there's no reason to be using CMAKE_EXE_LINKER_FLAGS.  What are you actually trying to accomplish? Because there is almost certainly a better way to achieve your desired result than via CMAKE_EXE_LINKER_FLAGS.

- Chuck

On Fri, May 20, 2016 at 4:28 AM, Chaos Zhang <[hidden email]> wrote:
Hi, all,

I try to use some gcc link option and libs(except libs link by
target_link_libraries(...)), i researched and try to use
'CMAKE_EXE_LINKER_FLAGS' in a simple project, as below:
in dir, there are 5 files:

-hello.h
-hello.c
-main.c
-hello.o
-libhello.a

hello.o was compiled by hello.c(gcc -c hello.c) and libhello.a contained
hello.o. And other 3 files content:
1.In hello.h:

void hello();

2.In hello.c:

#include<stdio.h>
void hello()
{
    printf("hello\n");
}

3.In main.c:

#include "hello.h"
void main()
{
   hello();
}

When i used gcc -o main main.c libhello.a, the exe file: main generated, and
it work well.
Then i write a CMakeLists.txt, the content as below:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
add_executable(main main.c)

When i cmake&make this project, an error occurred: main.c:undefined
reference to 'hello'.

Since i wil add a complicated link option and would not conflict with  libs
link by target_link_libraries(...), i should figure out how to use
'CMAKE_EXE_LINKER_FLAGS' or other ways, could you please give me some
advices? :-)

Thanks,
Chao



--
View this message in context: http://cmake.3232098.n2.nabble.com/How-to-use-CMAKE-EXE-LINKER-FLAGS-correctly-tp7593495.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 | Threaded
Open this post in threaded view
|

Re: How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chaos Zhang
Nice to see you again, Mr. Atkins. I was trying to add a very long gcc link option to a ELF file, there are some options, like -whole-archive, -rpath,and i don't known how to set in CMake, so i decided to use original gcc option, is there better way to solve this?
Chuck Atkins wrote
Is there a reason to not use target_link_libraries here? For the purposes
of your example there's no reason to be using CMAKE_EXE_LINKER_FLAGS.  What
are you actually trying to accomplish? Because there is almost certainly a
better way to achieve your desired result than via CMAKE_EXE_LINKER_FLAGS.

- Chuck

On Fri, May 20, 2016 at 4:28 AM, Chaos Zhang <[hidden email]> wrote:

> Hi, all,
>
> I try to use some gcc link option and libs(except libs link by
> target_link_libraries(...)), i researched and try to use
> 'CMAKE_EXE_LINKER_FLAGS' in a simple project, as below:
> in dir, there are 5 files:
>
> -hello.h
> -hello.c
> -main.c
> -hello.o
> -libhello.a
>
> hello.o was compiled by hello.c(gcc -c hello.c) and libhello.a contained
> hello.o. And other 3 files content:
> 1.In hello.h:
>
> void hello();
>
> 2.In hello.c:
>
> #include<stdio.h>
> void hello()
> {
>     printf("hello\n");
> }
>
> 3.In main.c:
>
> #include "hello.h"
> void main()
> {
>    hello();
> }
>
> When i used gcc -o main main.c libhello.a, the exe file: main generated,
> and
> it work well.
> Then i write a CMakeLists.txt, the content as below:
>
> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
> add_executable(main main.c)
>
> When i cmake&make this project, an error occurred: main.c:undefined
> reference to 'hello'.
>
> Since i wil add a complicated link option and would not conflict with  libs
> link by target_link_libraries(...), i should figure out how to use
> 'CMAKE_EXE_LINKER_FLAGS' or other ways, could you please give me some
> advices? :-)
>
> Thanks,
> Chao
>
>
>
> --
> View this message in context:
> http://cmake.3232098.n2.nabble.com/How-to-use-CMAKE-EXE-LINKER-FLAGS-correctly-tp7593495.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 | Threaded
Open this post in threaded view
|

Re: How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chuck Atkins
 
-rpath

 RPATHs are automatically added by CMake to executables so they can use libraries from the build tree.

 
-whole-archive

whole-archive is definitely trickier since you shouldn't be applying it to the entire executable but instead wrapping individual libraries with it.  Conveniently, you can pass link options directly with target_link_libraries.  So you could have:

# Just an example, find_library calls should really be isolated to separate find modules
find_library(FOO_LIBRARY foo)
set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY} -Wl,--no-whole-archive")

add_executable(hello main.c)
target_link_libraries(hello ${FOO_LIBRARY})
 

>> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")

CMAKE_EXE_LINKER_FLAGS should work for other more general cases that aren't tied to specific libraries.  The problem with your example is likely not using an absolute path for -L since the compilation is actually taking place in a nested work directory somewhere.


--

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 | Threaded
Open this post in threaded view
|

Re: How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chaos Zhang
Thanks a lot! `set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY} -Wl,--no-whole-archive")` worked when the content of ${FOO_LIBRARY} look like `-Ldir -llib`, if just use lib name as the content of ${FOO_LIBRARY}, error while occur.
Chuck Atkins wrote
> -rpath


 RPATHs are automatically added by CMake to executables so they can use
libraries from the build tree.



> -whole-archive
>

whole-archive is definitely trickier since you shouldn't be applying it to
the entire executable but instead wrapping individual libraries with it.
Conveniently, you can pass link options directly with
target_link_libraries.  So you could have:

# Just an example, find_library calls should really be isolated to separate
find modules
find_library(FOO_LIBRARY foo)
set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY} -Wl,--no-whole-archive")

add_executable(hello main.c)
target_link_libraries(hello ${FOO_LIBRARY})


>> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
>

CMAKE_EXE_LINKER_FLAGS should work for other more general cases that aren't
tied to specific libraries.  The problem with your example is likely not
using an absolute path for -L since the compilation is actually taking
place in a nested work directory somewhere.

--

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 | Threaded
Open this post in threaded view
|

Re: How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chuck Atkins
If you know the full path to the library then I'd suggest using it as /path/to/foo/libfoo.a (or .so if it's shared) instead of -L/path/to/foo -lfoo.  CMake will adjust the flags to -L and -l if needed but the preference is always to use the full library path.

- Chuck

On Mon, May 23, 2016 at 2:23 AM, Chaos Zhang <[hidden email]> wrote:
Thanks a lot! `set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY}
-Wl,--no-whole-archive")` worked when the content of ${FOO_LIBRARY} look
like `-Ldir -llib`, if just use lib name as the content of ${FOO_LIBRARY},
error while occur.

Chuck Atkins wrote
>> -rpath
>
>
>  RPATHs are automatically added by CMake to executables so they can use
> libraries from the build tree.
>
>
>
>> -whole-archive
>>
>
> whole-archive is definitely trickier since you shouldn't be applying it to
> the entire executable but instead wrapping individual libraries with it.
> Conveniently, you can pass link options directly with
> target_link_libraries.  So you could have:
>
> # Just an example, find_library calls should really be isolated to
> separate
> find modules
> find_library(FOO_LIBRARY foo)
> set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY}
> -Wl,--no-whole-archive")
>
> add_executable(hello main.c)
> target_link_libraries(hello ${FOO_LIBRARY})
>
>
>>> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
>>
>
> CMAKE_EXE_LINKER_FLAGS should work for other more general cases that
> aren't
> tied to specific libraries.  The problem with your example is likely not
> using an absolute path for -L since the compilation is actually taking
> place in a nested work directory somewhere.
>
> --
>
> 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





--
View this message in context: http://cmake.3232098.n2.nabble.com/How-to-use-CMAKE-EXE-LINKER-FLAGS-correctly-tp7593495p7593518.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 | Threaded
Open this post in threaded view
|

Re: How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chaos Zhang
This CMake  feature is interesting, the first step i plan is to make the project work first, then i will try to optimize it, and i think this feature would be used next step. Thanks. :-)
Chuck Atkins wrote
If you know the full path to the library then I'd suggest using it as
/path/to/foo/libfoo.a (or .so if it's shared) instead of -L/path/to/foo
-lfoo.  CMake will adjust the flags to -L and -l if needed but the
preference is always to use the full library path.

- Chuck

On Mon, May 23, 2016 at 2:23 AM, Chaos Zhang <[hidden email]> wrote:

> Thanks a lot! `set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY}
> -Wl,--no-whole-archive")` worked when the content of ${FOO_LIBRARY} look
> like `-Ldir -llib`, if just use lib name as the content of ${FOO_LIBRARY},
> error while occur.
>
> Chuck Atkins wrote
> >> -rpath
> >
> >
> >  RPATHs are automatically added by CMake to executables so they can use
> > libraries from the build tree.
> >
> >
> >
> >> -whole-archive
> >>
> >
> > whole-archive is definitely trickier since you shouldn't be applying it
> to
> > the entire executable but instead wrapping individual libraries with it.
> > Conveniently, you can pass link options directly with
> > target_link_libraries.  So you could have:
> >
> > # Just an example, find_library calls should really be isolated to
> > separate
> > find modules
> > find_library(FOO_LIBRARY foo)
> > set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY}
> > -Wl,--no-whole-archive")
> >
> > add_executable(hello main.c)
> > target_link_libraries(hello ${FOO_LIBRARY})
> >
> >
> >>> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
> >>
> >
> > CMAKE_EXE_LINKER_FLAGS should work for other more general cases that
> > aren't
> > tied to specific libraries.  The problem with your example is likely not
> > using an absolute path for -L since the compilation is actually taking
> > place in a nested work directory somewhere.
> >
> > --
> >
> > 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
>
>
>
>
>
> --
> View this message in context:
> http://cmake.3232098.n2.nabble.com/How-to-use-CMAKE-EXE-LINKER-FLAGS-correctly-tp7593495p7593518.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