简体   繁体   中英

No warning from CMake when not including sources with `target_sources`

I have a project which depends on foo.cpp

add_library(App SHARED app.cpp)
target_link_libraries(App Module)
target_sources(App PRIVATE foo.cpp)
add_executable(app main.cpp)
target_link_libraries(app App)

which works perfectly. However if I remove line 3 to have

add_library(App SHARED app.cpp)
target_link_libraries(App Module)
add_executable(app main.cpp)
target_link_libraries(app App)

Then CMake does not give any warning in the either the configure or build stage. The only error I get is when running the eventual binary

./build/src/app/app: symbol lookup error: ./build/src/app/app: undefined symbol: _Z5startiPPc

Can I avoid this to get warnings/errors more up-front?

Can I avoid this to get warnings/errors more up-front?

Do you mean you want to get those errors during the build stage? You can't. That is a loader error, which basically means that it was unable to find the referenced symbol because the library was not found or something. And no there is no way to avoid it.

Suppose you build and run an application which runs correctly and then you go ahead and move or delete the libraries it is linking to you will suddenly get this error. As you can imagine, no one can warn you about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM