# Native binary cpp: add linker args in prebuilt library section

**URL:** <https://discuss.gradle.org/t/native-binary-cpp-add-linker-args-in-prebuilt-library-section/11080>\
**Category:** Help/Discuss\
**Created:** [August 9, 2015, 5:56pm UTC](https://discuss.gradle.org/t/native-binary-cpp-add-linker-args-in-prebuilt-library-section/11080 "2015-08-09T17:56:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RalfSchallenberg](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@RalfSchallenberg](https://discuss.gradle.org/u/RalfSchallenberg)\
**Post date:** [August 9, 2015, 5:56pm UTC](https://discuss.gradle.org/t/native-binary-cpp-add-linker-args-in-prebuilt-library-section/11080/1 "2015-08-09T17:56:09Z")

</div>

I’m trying to add the boost library to my repositories as a prebuilt library and don’t want to make one prebuilt library for each of the boost sub-libs that come with a static lib. Since the boost sub-libs that come with a static lib know how to link theirs libs (via pre-processor #pragmas) my idea was to just add LIBPATH pointing to these static libs to the linker args like this:

```
repositories {
    libs(PrebuiltLibraries) {
        boost {
            headers.srcDir "src/3rdParty/boost_1_57_0/boost"
            binaries.all {
                def arch = (targetPlatform == platforms.x86) ? "Win32" : "x64";
                def type = (buildType == buildTypes.debug) ? "Debug" : "Release";
                linker.args << "/LIBPATH:src/3rdParty/boost_1_57_0/Lib/${arch}_${type}"
            }
        }
...

```

This doesn’t work, because I can’t access `linker` from this scope. Is there another way to solve this?

---

<div class="post-metadata">

**Author:** ![daz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daz/32/2475_2.png) [@daz](https://discuss.gradle.org/u/daz)\
**Post date:** [August 10, 2015, 5:59pm UTC](https://discuss.gradle.org/t/native-binary-cpp-add-linker-args-in-prebuilt-library-section/11080/2 "2015-08-10T17:59:12Z")

</div>

> [@RalfSchallenberg](#):
>
> I’m trying to add the boost library to my repositories as a prebuilt library and don’t want to make one prebuilt library for each of the boost sub-libs that come with a static lib

What’s your reason for not wanting to create separate prebuilt library instances? The usual way to do this would be to iterate over the libraries on disk and configure a prebuilt library for each.

---

<div class="post-metadata">

**Author:** ![RalfSchallenberg](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@RalfSchallenberg](https://discuss.gradle.org/u/RalfSchallenberg)\
**Post date:** [August 10, 2015, 7:14pm UTC](https://discuss.gradle.org/t/native-binary-cpp-add-linker-args-in-prebuilt-library-section/11080/3 "2015-08-10T19:14:55Z")

</div>

> [@daz](#):
>
> What’s your reason for not wanting to create separate prebuilt library instances?

Because normally (when not using Gradle but Visual Studio) I don’t have to. All I need to do is tell the compiler where the boost headers are, and the linker where the libs are. Not each lib individually, just the directory where all lib files are stored. Then when I include e.g. boost/thread.hpp, the linker will automatically be told to link the corresponding lib file. I thought I could replicate that with Gradle.

> [@daz](#):
>
> The usual way to do this would be to iterate over the libraries on disk and configure a prebuilt library for each.

That sounds interesting. Can you point me to an example? I’m new to Gradle and don’t know the usual tricks.

---

<div class="post-metadata">

**Author:** ![daz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daz/32/2475_2.png) [@daz](https://discuss.gradle.org/u/daz)\
**Post date:** [August 11, 2015, 4:33pm UTC](https://discuss.gradle.org/t/native-binary-cpp-add-linker-args-in-prebuilt-library-section/11080/4 "2015-08-11T16:33:05Z")

</div>

I don’t have an example on hand, but if you know a bit of Groovy you can iterate over the directories or files, and configure a prebuilt library for each with the normal syntax.

Completely untested code:::

```
repositories {
    libs(PrebuiltLibraries) { prebuilt ->
        file('boost').listFiles().each { subDir ->
            prebuilt."boost-${subDir.name}" {
                headers.srcDir "${subDir.absolutePath}/headers"
                ...
            }
        }
```
