Skip to content

Commit

Permalink
FAQ: Add entry about add_project_link_arguments and multiple languages
Browse files Browse the repository at this point in the history
This seems to come up fairly often, so having an FAQ entry seems useful.
  • Loading branch information
dcbaker authored and eli-schwartz committed Nov 2, 2021
1 parent b726241 commit 98a9cc0
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/markdown/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,47 @@ tested, generally don't regress, and users are more likely to have domain
knowledge about them. They also tend to have better tooling (such as
autocompletion, linting, testing solutions), which make them a lower
maintenance burden over time.

## Why don't the arguments passed to `add_project_link_arguments` affect anything?

Given code like this:
```meson
add_project_link_arguments(['-Wl,-foo'], language : ['c'])
executable(
'main',
'main.c',
'helper.cpp',
)
```

One might be surprised to find that `-Wl,-foo` is *not* applied to the linkage
of the `main` executable. In this Meson is working as expected, since meson will
attempt to determine the correct linker to use automatically. This avoids
situations like in autotools where dummy C++ sources have to be added to some
compilation targets to get correct linkage. So in the above case the C++ linker
is used, instead of the C linker, as `helper.cpp` likely cannot be linked using
the C linker.

Generally the best way to resolve this is to add the `cpp` language to the
`add_project_link_arguments` call.
```meson
add_project_link_arguments(['-Wl,-foo'], language : ['c', 'cpp'])
executable(
'main',
'main.c',
'helper.cpp',
)
```

To force the use of the C linker anyway the `link_language` keyword argument can
be used. Note that this can result in a compilation failure if there are symbols
that the C linker cannot resolve.
```meson
add_project_link_arguments(['-Wl,-foo'], language : ['c'])
executable(
'main',
'main.c',
'helper.cpp',
link_language : 'c',
)
```

0 comments on commit 98a9cc0

Please sign in to comment.