[CMake] CMake builds library/executable out of order
Schuchard, Matthew
Matthew.Schuchard at gtri.gatech.edu
Wed Dec 7 10:02:24 EST 2011
Please note:
> I should mention I have library prefixes and suffixes turned off for
> both linking and building, meaning that the full filename of libraries
> are specified (hence the libfoo.a and not just foo).
Also a side consequence is I specify linked libraries by filepath and not target names, which has worked fine thus far.
-----Original Message-----
From: David Cole [mailto:david.cole at kitware.com]
Sent: Wednesday, December 07, 2011 9:53 AM
To: Schuchard, Matthew
Cc: cmake at cmake.org
Subject: Re: [CMake] CMake builds library/executable out of order
On Wed, Dec 7, 2011 at 9:37 AM, Schuchard, Matthew <Matthew.Schuchard at gtri.gatech.edu> wrote:
> I am having this strange issue with building a library and then
> linking it to an executable.
>
>
>
> If I have the following in my CMakeList:
>
>
>
> add_library(libfoo.a ${srcfiles})
>
> add_executable(foo_exec.Fx foo_exec.F)
>
> target_link_libraries(foo_exec.Fx libfoo.a)
>
>
>
> I get an error returned of "no rule to make target libfoo.a needed by
> foo_exec.Fx."
>
> I should mention I have library prefixes and suffixes turned off for
> both linking and building, meaning that the full filename of libraries
> are specified (hence the libfoo.a and not just foo).
>
>
>
> But if I change my CMakeList to:
>
>
>
> add_library(libfoo.a ${srcfiles})
>
> #add_executable(foo_exec.Fx foo_exec.F)
>
> #target_link_libraries(foo_exec.Fx libfoo.a)
>
>
>
> It builds libfoo.a without any complaint. When I uncomment the
> executable lines after building libfoo.a via the above, the library is
> linked to the executable with no complaints.
>
> I might also mention since the Fortran compiler used is ABSoft and I
> am building on a 64-bit Redhat system, the linker I am using for
> Fortran executables is g++, but that may be irrelevant.
>
>
>
> How do I specify that the library needs to be built before the
> executable which links to it, so that CMake does not return an error
> when it tries to build and link the executable before building its dependent library?
>
> This almost seems like a bug in CMake.
Try this:
add_library(foo ${srcfiles})
add_executable(foo_exec.Fx foo_exec.F)
target_link_libraries(foo_exec.Fx foo)
CMake will automatically add the "lib" prefix and the ".a" suffix to the actually built library file. "foo" is the CMake target name for the libfoo.a library in this case.
I don't know why it should matter, but perhaps the target name "libfoo.a" is confounding things somehow.
HTH,
David
More information about the CMake
mailing list