Linking libraries only on Windows


(Arjan Broos) #1

I’m using Gradle to try and build an SFML application across Windows and Linux. When linking SFML statically, the Windows version needs a few other libraries to be linked. How do I make sure that these are linked only for Windows?

Here’s my gradle.build file so far:

apply plugin: 'cpp'

model {
	repositories {
        libs(PrebuiltLibraries) {
            sfml {
                headers.srcDir "../SFML-2.4.2/include"
            }

            def libDir = "../SFML-2.4.2/lib/"
            def libPrefix = "lib"
            def libSuffix = "-s.a"
            if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
            	libPrefix = ""
				libSuffix = "-s.lib"

                opengl32 {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'opengl32.lib')
                    }
                }

                winmm {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'winmm.lib')
                    }
                }

                gdi32 {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'gdi32.lib')
                    }
                }

                user32 {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'user32.lib')
                    }
                }

                advapi32 {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'advapi32.lib')
                    }
                }

                freetype {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'freetype.lib')
                    }
                }

                jpeg {
                    binaries.withType(PrebuiltStaticLibraryBinary) {
                        staticLibraryFile = file(libDir + 'jpeg.lib')
                    }
                }
			}

            sfmlgraphics {
                binaries.withType(PrebuiltStaticLibraryBinary) {
                    staticLibraryFile = file(libDir + libPrefix + 'sfml-graphics' + libSuffix)
                }
            }

            sfmlwindow {
                binaries.withType(PrebuiltStaticLibraryBinary) {
                    staticLibraryFile = file(libDir + libPrefix + 'sfml-window' + libSuffix)
                }
            }

            sfmlsystem {
                binaries.withType(PrebuiltStaticLibraryBinary) {
                    staticLibraryFile = file(libDir + libPrefix + 'sfml-system' + libSuffix)
                }
            }
        }
	}

    components {
		MicroVis(NativeExecutableSpec) {
			binaries.all {
                cppCompiler.define "SFML_STATIC"
				cppCompiler.args '-Wall', '-MD'
			}
			sources {
				cpp {
					source {
						srcDir "src/MicroVis"
						include "**/*.cpp"
					}

					exportedHeaders {
						srcDir 'include'
					}

					lib library: "sfml", linkage: "api"
					lib library: "sfmlgraphics", linkage: "static"
					lib library: "sfmlwindow", linkage: "static"
					lib library: "sfmlsystem", linkage: "static"

                    lib library: "opengl32", linkage: "static"
                    lib library: "winmm", linkage: "static"
                    lib library: "gdi32", linkage: "static"
                    lib library: "user32", linkage: "static"
                    lib library: "advapi32", linkage: "static"
                    lib library: "freetype", linkage: "static"
                    lib library: "jpeg", linkage: "static"
				}
			}
		}
	}
}

Thanks for any help!