How to specify creation of C library


(Michael Shulman) #1

Hi, I am an absolute beginner. I’m trying to create a C static library on Windows, after having read much documentation. Here is my build.gradle contents:

/*

  • Create Windows static C library myLib.lib
    */

apply plugin: ‘c’

model {

components {
	myLib(NativeLibrarySpec) {
		sources {
			c {
				source {
					srcDirs "src/cpp"
					include "*.c"
				}
			}
		}
	}
}

binaries {
	withType(StaticLibraryBinarySpec) {
		if (toolChain in VisualCpp) {
			cppCompiler.define "DLL_EXPORT"
			cppCompiler.args "/DIBMRS6000"
		}
	}

}

}

When I type “gradlew irpGrouper64StaticLibrary”, it reports a number of C compile errors. Analysis tells me that these can be avoided by specifying a command-line compiler flag of /DIBMRS6000, as an equivalent to #define IBMRS6000 in all files. As you see, I put that flag into a line cppComputer.args, but apparently that was not the right way to do that. Any suggestions would be welcomed.

Thanks in advance, mshulman256 (Michael Shulman)


(Michael Shulman) #2

I’ve solved the first problem, but that revealed the second problem.

Given that I was importing plugin:‘c’

I changed the following in build.gradle:
withType(StaticLibraryBinarySpec) {
if (toolChain in VisualCpp) {
cppCompiler.define “DLL_EXPORT”
cppCompiler.args “/DIBMRS6000”
}
}
to this:
withType(StaticLibraryBinarySpec) {
if (toolChain in VisualCpp) {
cCompiler.define “DLL_EXPORT”
cCompiler.args “/DIBMRS6000”
}
}
and now symbol IBMRS6000 is defined for all sources in the static library build.
But the symbol is NOT defined for sources in the shared library build.
The BIG QUESTION: What do I have to add, to specify compiler args for the shared library build?


(Michael Shulman) #3

Success! I realized that I had to insert a stanza:

withType(SharedLibraryBinarySpec)

with the same contents as the stanza:

withType(StaticLibraryBinarySpec)

so now both libraries build successfully.