# \[CPP\] Conditional Linking and Excluding

**URL:** <https://discuss.gradle.org/t/cpp-conditional-linking-and-excluding/10295>\
**Category:** Help/Discuss\
**Created:** [June 25, 2015, 4:36pm UTC](https://discuss.gradle.org/t/cpp-conditional-linking-and-excluding/10295 "2015-06-25T16:36:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MyNameIsJulien](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@MyNameIsJulien](https://discuss.gradle.org/u/MyNameIsJulien)\
**Post date:** [June 25, 2015, 4:36pm UTC](https://discuss.gradle.org/t/cpp-conditional-linking-and-excluding/10295/1 "2015-06-25T16:36:07Z")

</div>

Since I found Gradle’s DSL to be comprehensible,  
I decided to use it instead of CMake. I was able to build somefiles within Gradle but need help.

My project structure looks like this:

```
Noir/
    build.gradle
    settings.gradle
    engine/
        build.gradle
        Noir/
            headers
            cpp
        Noir.Graphics/
        Noir.Sound/
    editor/
        Nothing yet

```

My Noir.Graphics component, does require certain libraries to be linked.  
For windows I need “User32.dll”, “OpenGL32.dll” to be linked,  
on linux I need X11 and MesaGLDev (I currently don’t now the libraries name).

My sound Component will need certain libraries too.

On the other hand, I’ll need to exclude certain files, which are used in conjunction with the PIMPL-Pattern (also known as Bridge-Pattern). These files are merely implementations for every platform.

How can this be achieved the Gradle DSL, since I am fairly new to it?  
That’s my current gradle file:

```
apply plugin: 'cpp'

model{
    components{
        Noir(SharedLibraryBinarySpec)
        Noir.Graphics(SharedLibraryBinarySpec)
        Noir.Sound(SharedLibraryBinarySpec)
    }
}

```

P.S.:  
Who ever came up with the idea having two folders (headers, cpp), I admire it.

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [June 27, 2015, 7:25am UTC](https://discuss.gradle.org/t/cpp-conditional-linking-and-excluding/10295/2 "2015-06-27T07:25:56Z")

</div>

For libraries/dependencies that Gradle doesn’t build, your two main options are to model them as prebuilt libraries: [https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/native-binaries/prebuilt](https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/native-binaries/prebuilt)

or to add the arguments required to use them by hand for now (this makes the most sense for system libraries). i.e., You’d depend on the environment having the correct search paths and you’d say:

```gradle
binaries.all {
   linker.args '-lopengl32'
}

```

WRT having separate platform specific implementations of things, you’d do something like this:

> **[gradle/gradle](https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/native-binaries/assembler)**
>
> gradle - Adaptable, fast automation for all

You basically selectively add new source sets based on the variant being built.

HTH

---

<div class="post-metadata">

**Author:** ![MyNameIsJulien](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@MyNameIsJulien](https://discuss.gradle.org/u/MyNameIsJulien)\
**Post date:** [June 27, 2015, 4:42pm UTC](https://discuss.gradle.org/t/cpp-conditional-linking-and-excluding/10295/3 "2015-06-27T16:42:01Z")

</div>

Thanks for your answer.  
Currently I am using the second approach since I have troubles understanding  
how to configure prebuildt libraries.

I have adapted my buildfile a bit and fixed some errors, which came from my lack of knowledge of gradle’s DSL.  
Will there be an official tutorial?  
The tutorials presented on the “Cpp Build Operator” Page are deprecated and the Gradle Docs are kind off unclear (still, in comparison to CMake I got something to build 😛 ).

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [June 27, 2015, 7:24pm UTC](https://discuss.gradle.org/t/cpp-conditional-linking-and-excluding/10295/4 "2015-06-27T19:24:12Z")

</div>

Yes, breaking up and improving the documentation is one of the things we want to do. The samples and the [user guide chapter](https://docs.gradle.org/current/userguide/nativeBinaries.html) are the most we have right now.
