c++ - Gradle-Visual C++编译器,vcpkg和外部库

标签 c++ visual-studio gradle vcpkg

我在vcpkg旁边安装了Visual Studio 2019。我已经安装了一个外部依赖项(使用vcpkg install msmpi:x86-windows的msmpi),并尝试使用Visual Studio IDE创建示例MPI项目:一切正常,不需要任何其他配置,令人印象深刻。

现在-由于我正在从事的项目的性质,我想使用gradle编译我的代码(在Visual Studio外部)。为此,我使用了Gradle的cpp-application插件以及以下build.gradle:

plugins {
    id 'cpp-application'
}

简单的“Hello world”的编译有效:gradle找到Visual C++编译器,执行该编译器,一切正常运行(顺便说一句,我也印象深刻)。

当我将外部库的 header (mpi.h)包含到我的代码中时,就会出现问题。当使用gradle和cpp-application插件进行编译时,似乎看不到通过vcpkg预先安装的依赖项(当我使用Visual Studio IDE编译代码时,不需要任何其他配置即可进行的所有工作)。如何解决该问题,最好在我的build.gradle中没有硬编码库和 header 的情况下解决?

最佳答案

通过向其提供vcpgk安装的mpi header 和库的路径,我能够获得编译我的项目的机会。我将寻找一种使其更灵活的方法。不过,这是我对build.gradle的补充:

ext {
    vcpgkIncludePath = 'path-to-include'
    vcpkgLibPath = 'path-to-vcpkg-libs'
}
tasks.withType(CppCompile).configureEach {
    compilerArgs.addAll toolChain.map { toolChain ->
        if (toolChain in VisualCpp) {
            return ["/I$vcpgkIncludePath"]
        }
        return []
    }
}

tasks.withType(org.gradle.nativeplatform.tasks.LinkExecutable).configureEach {
    linkerArgs.addAll toolChain.map { toolChain ->
        if (toolChain in VisualCpp) {
            return ["/LIBPATH:$vcpkgLibPath", "msmpi.lib"]
        }
        return []
    }
}

关于c++ - Gradle-Visual C++编译器,vcpkg和外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365089/

相关文章:

regex - 如何替换 Notepad++ /Visual Studio中的特定模式

C++/CLI - 调用 DLL 中定义的非托管函数

c++ - MFC - 如何绘制彩色位图到对话框DC

c++ - 如何将字符串解析为已知结构 C++ 中的数字

c - 使用 Fortran 共享库时名称不匹配

android - 无法解决 - android maps utils

c++ - 如何为 std::fill() 使用 C++0x lambdas 局部变量?

c++ - 在 VS Community 中编写基本内容时出现奇怪错误 - C++

android - 无法解决:com.github.mopa11:MainDemo1:1.0.0

gradle - Gradlew-正确的JAVA_HOME路径无效