Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the meaning of add_library (*** ALIAS ***) #70

Open
kuazhangxiaoai opened this issue Sep 12, 2021 · 3 comments
Open

What's the meaning of add_library (*** ALIAS ***) #70

kuazhangxiaoai opened this issue Sep 12, 2021 · 3 comments

Comments

@kuazhangxiaoai
Copy link

add_library(hello::library ALIAS hello_library)

why must to use alias name for hello_library? Can I not use the alias name?

@ttroy50
Copy link
Owner

ttroy50 commented Sep 14, 2021

It provides an alternative way to reference a target name in other parts of your cmake files.

In that example you have

add_library(hello_library SHARED
    src/Hello.cpp
)
add_library(hello::library ALIAS hello_library)

hello::library refers to the same library as hello_library but gives it a different name.

One good use case that I have found for this is in the target_link_libraries command. If you use an alias target like

target_link_libraries(hello_binary
    PRIVATE
        hello::library
)

and some some reason the hello::library target doesn't exist you will get a failure. If instead you had used

target_link_libraries(hello_binary
    PRIVATE
        hello_library
)

And you didn't have a hello_library target, than you could accidentally link a library libhello_library.so that already exists on your filesystem.

@ttroy50
Copy link
Owner

ttroy50 commented Sep 14, 2021

Here is a link to the cmake documentation on alias targets

https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#alias-targets

@nevalsar
Copy link

@ttroy50 I don't believe this is a valid use case for this feature... The accidental linking can directly be avoided using the original hello_library itself by linking it like so:

target_link_libraries(hello_binary
    PRIVATE
        hello_library::hello_library
)

If the target isn't found, this would fail (which is what we want) instead of looking for a library in the filesystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants