[CMake] How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chaos Zhang zcsd2012 at gmail.com
Fri May 20 04:28:38 EDT 2016


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.


More information about the CMake mailing list