Cmake: Use imported object
19:47 14 Jul 2018

Although this duplicate is a very similar question, it predates the methodology I'm asking about ("imported object libraries"), which was added to Cmake in version 3.9.6 ~1 year ago(?) hence none of the answers there address this.

I've just started using cmake and I'm trying to more or less replicate this kind of behaviour from a makefile:

foo: foo.cpp bar.o
    $(CXX) $^ -o $@

bar.o:
    cp $(EXTERNAL_DIR)/bar.o .

So bar.o is just being copied in from outside. It's not being built, etc. Then it's compiled into the foo target.

In the Cmake documentation this seems pretty straightforward; to import the .o file:

add_library(bar OBJECT IMPORTED GLOBAL)
set_property(TARGET bar PROPERTY IMPORTED_LOCATION ${EXTERNAL_DIR}/bar.o)

And to use it:

add_executable(foo $ foo.cpp) 

But when the build runs, I get all missing symbols for the stuff that's supposed to be in bar.o -- and run verbosely, it's clear why: Cmake never uses it for anything. It's not mentioned in the compiler invocation, and grepping through the build directories, it's not mentioned in any of the generated makefiles either.

Further, I can change the IMPORTED_LOCATION to something non-existent and it doesn't care. It runs exactly the same way.

What have I misunderstood here? How do I get this done (and without having to add anything to EXTERNAL_DIR)?

c++ makefile cmake