gpt4 book ai didi

c++ - 使用 CPP 插件在 gradle 中编译 C++ 代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:33 24 4
gpt4 key购买 nike

我正在研究 gradle 脚本,我正在尝试编译 C++ 代码,我的结构如下。我正在尝试在下面的机器上编译我的代码,它显示 C++ 编译器失败,而我没有遇到问题与 maven 可以 somel

Linux 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 Sun 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux

└───src
└───main
├───c++
│ ├───headers (headres is having **.h files)
│ └───native (native contains **.cpp files)
└───resources
└───DSresources
└───DSLib

apply plugin: 'cpp'
//-- set the group for publishing
group = 'com.rohit.singh'
/**
* Initializing GAVC settings
*/
def buildProperties = new Properties()
file("version.properties").withInputStream {
stream -> buildProperties.load(stream)
}
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.engineBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.engineBuildVersion
println "${version}"

//name is set in the settings.gradle file
group = "com.rohit.singh"
version = buildProperties.engineBuildVersion
println "Building ${project.group}:${project.name}:${project.version}"

model {
components {
main(NativeExecutableSpec) {
targetPlatform "x86"
targetPlatform "x64"
sources {
cpp {
source {
srcDir "src/main/c++/native"
}
}
}
}
}
}

下面是maven代码片段

profile>
<id>Linux</id>
<activation>
<os>
<family>Linux</family>
</os>
</activation>
<properties>
<packaging.type>so</packaging.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>1.0-alpha-8</version>
<extensions>true</extensions>
<configuration>

<javahOS>linux</javahOS>

<compilerProvider>generic-classic</compilerProvider>
<compilerExecutable>g++</compilerExecutable>
<linkerExecutable>g++</linkerExecutable>
<sources>
<source>
<directory>NativeJNI/../src/main/c++/native</directory>
<fileNames>
<fileName>JniSupport.cpp</fileName>
<fileName>DiseaseStagingJni.cpp</fileName>
</fileNames>
</source>
</sources>
<compilerStartOptions>
<compilerStartOption>-fPIC</compilerStartOption>
</compilerStartOptions>
<linkerFinalName>NativeJNI</linkerFinalName>
<linkerStartOptions>
<linkerStartOption>-shared -L${basedir}/src/main/resources/DSresources/DSLib -lds64 -Wl,-rpath,${basedir}/src/main/resources/DSresources/DSLib</linkerStartOption>
</linkerStartOptions>

</configuration>
<executions>
<execution>
<id>javah</id>
<phase>generate-sources</phase>
<configuration>
<finalName>LinuxNativeJNI</finalName>
<javahOS>linux</javahOS>
<javahProvider>default</javahProvider>
<javahOutputDirectory>${project.build.directory}/custom-javah</javahOutputDirectory>
<workingDirectory>${basedir}</workingDirectory>
<javahOutputFileName>DiseaseStagingJniWrapper.h</javahOutputFileName>
<javahClassNames>
<javahClassName>com.truvenwealth.analyticsengine.common.diseasestaging.DiseaseStagingJniWrapper</javahClassName>
</javahClassNames>
</configuration>
<goals>
<goal>javah</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>

最佳答案

我按以下方式使用了 Javah 函数。

apply plugin: 'cpp'
apply plugin: 'java'
//-- set the group for publishing
group = 'com.rohit.singh'

/**
* Initializing GAVC settings
*/
def buildProperties = new Properties()
file("version.properties").withInputStream {
stream -> buildProperties.load(stream)
}
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.ncdefBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.anal
println "${version}"

//name is set in the settings.gradle file
group = "com.rohit.singh"
version = buildProperties.anal
println "Building ${project.group}:${project.name}:${project.version}"

repositories {
maven {
url "http://xxx.tsh.xxon.com:x/factory/libslocal"
}
maven {
url "http://xxx.tsh.xxon.com:x/factory/libs-release"
}
}
dependencies {
compile ([
"com.rohit.singh:engine-common:4.+"
])
}



model {
repositories {
libs(PrebuiltLibraries) {
jdk {
headers.srcDirs "${System.properties['java.home']}/../include",
"${System.properties['java.home']}/../include/win32",
"${System.properties['java.home']}/../include/darwin",
"${System.properties['java.home']}/../include/linux"
}
}
}
}

model {
platforms {
x64 { architecture "x86_64" }
x86 { architecture "x86" }
}
}

model {
components {
main(NativeLibrarySpec) {
sources {
cpp {
source {
lib library: 'main', linkage: 'static'
lib library: 'jdk', linkage: 'api'
srcDir "src/main/c++/native"
include "**/JniSupport.cpp"
include "**/DiseaseStagingJni.cpp"
}
}
}
}
}
}

def nativeHeadersDir = file("$buildDir/nativeHeaders")
//def compilePath = configurations.compile.resolve().collect {it.absolutePath}.join(";")
binaries.all {
// Define toolchain-specific compiler and linker options
if (toolChain in Gcc) {
cppCompiler.args "-I${nativeHeadersDir}"
cppCompiler.args "-g"
linker.args '-Xlinker', '-shared -LNativeJNI/src/main/resources/DSresources/DSLib -lds64 -Wl'
}
}

**//def nativeHeadersDir = file("$buildDir/nativeHeaders")
task nativeHeaders {
// def nativeHeadersDir = file("$buildDir/nativeHeaders")
def outputFile = file("$nativeHeadersDir/DiseaseStagingJniWrapper.h")
def classes = [
'com.abcedefgh.nice.common.diseasestaging.DiseaseStagingJniWrapper'
]
inputs.files sourceSets.main.output
inputs.property('classes', classes)
outputs.file outputFile
doLast {
outputFile.parentFile.mkdirs()
def compilePath = configurations.compile.resolve().collect {it.absolutePath}.join(":")
println "Using Compile Path: ${compilePath}"
exec {
executable org.gradle.internal.jvm.Jvm.current().getExecutable('javah')
args '-o', outputFile
args '-classpath', compilePath
args classes
}
}
}

tasks.withType(CppCompile) { task ->
task.dependsOn nativeHeaders
}**
//def filechange = file("NativeJNI-${project.version}.so")
//println filechange
task fixartifactname (type: Copy) {
//def filechange = "NativeJNI-${project.version}.so"
//println filechange
from 'build/binaries/mainSharedLibrary'
into 'build/libs'
def filechange = file("NativeJNI-${project.version}.so")
println filechange
include('libmain.so')
rename ('libmain.so', '${filechange}')
}
//println fixartifactname
build.dependsOn fixartifactname

关于c++ - 使用 CPP 插件在 gradle 中编译 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com