【问题标题】:Additional include paths and linker options Gradle for C++其他包含路径和链接器选项 Gradle for C++
【发布时间】:2020-04-14 08:20:14
【问题描述】:

我试图使用 Gradle 编译一个 C++ 项目,但我找不到在编译时声明我想要哪些附加包含路径以及在链接时附加哪些库的方法。 我在项目内部看到了涉及model { } 的解决方案(我不记得确切)。 (但没用,Gradle 一直在抱怨不存在的函数。)

似乎还不是一种将柯南依赖项添加到 Gradle 项目的清晰或简单的方法。这让我自己编译库,然后添加到构建系统,但同样,我找不到方法。

我一直在文档中寻找答案,但我发现的只是添加 maven 依赖项(但我猜 GLFW、GLEW 和 DearIMGUI 不在 Maven 上......)。

有什么怪癖吗?我怎么可能只注册额外的包含/链接文件?

我的build.gradle.kts

plugins {
    id("cpp-application")
}

tasks.register("runDebug") {
    doLast {
        exec {
            executable = "./build/exe/main/debug/my-app.exe"
        }
    }
}

【问题讨论】:

  • 我认为对于 gradle,你可能想要实现一个自定义生成器,因为对 Gradle 的支持不是内置的。有通用的json 生成器,它将把必要的信息转储到一个 json 文件中,你可以从那里得到它。这是你想要的?这篇博文可以帮助您进行新的集成:blog.conan.io/2019/07/24/…
  • @drodri 实际上,我正在寻找相反的方向,但是如果可以的话,那对我来说很好。我会寻找柯南集成,看看我发现了什么。 ~谢谢!
  • 你怎么看,我们应该写信给像 GLFW/GLEW 这样重要的原生库的开发人员,让他们在 maven 上发布它们吗?实际上我还没有找到一个本地库......
  • @MilTy 这可能很有趣,总是使用他们自己的方式来解决依赖关系很好,而不是不得不进行破解。

标签: c++ gradle build-system conan


【解决方案1】:

经过 5 个小时的搜索,终于找到了使用 Gradle 构建 QT 应用程序的解决方案。感谢@s-kadakov。

plugins {
    id 'cpp-application'
    id 'cpp-unit-test'
}

application {
    targetMachines.add(machines.linux.x86_64)
}

tasks.withType(CppCompile).configureEach {
    compilerArgs.add '-fPIC'
    includes {
        '/usr/include/qt'
    }

    compilerArgs.addAll toolChain.map { toolChain ->
        if (toolChain in [ Gcc, Clang ]) {
            return ['-O2', '-fno-access-control']
        } else if (toolChain in VisualCpp) {
            return ['/Zi']
        }
        return []
    }
}

tasks.withType(LinkExecutable).configureEach {
    linkerArgs.add '-v'

    linkerArgs.add '-lQt5Core'
    linkerArgs.add '-lQt5Widgets'
    linkerArgs.add '-lQt5Gui'

}

我希望这对你有帮助。

【讨论】:

    【解决方案2】:

    添加头目录或添加编译器选项;在 plugins 部分之后添加一个 application 部分,如下所示:

    application {
      privateHeaders {
       from('src/headers')
       from('/usr/include/qt5')
       }
       // To add compiler args do the following below
       binaries.configureEach {
          compileTask.get().compilerArgs.add('-O2')
       }
    }
    

    我仍在尝试如何添加库或链接器参数:(

    【讨论】:

      【解决方案3】:

      使用 Gradle 6.7,您可以执行以下操作:

      /*
       * This file was generated by the Gradle 'init' task.
       *
       * This generated file contains a sample C++ project to get you started.
       * For more details take a look at the Building C++ applications and libraries chapter in the Gradle
       * User Manual available at https://docs.gradle.org/6.7/userguide/building_cpp_projects.html
       */
      
      plugins {
          // Apply the cpp-application plugin to add support for building C++ executables
          id 'cpp-application'
      }
      
      // Set the target operating system and architecture for this application
      application {
          targetMachines.add(machines.linux.x86_64)
          
          // You may add extra private headrs 
          //privateHeaders {
          //  from('src/headers')
          //}    
      }
      
      // Some compiler configuration
      tasks.withType(CppCompile).configureEach {
          // Define a preprocessor macro for every binary
          macros.put("NDEBUG", null)
      
          // Define a compiler options
          compilerArgs.add '-W3'
      
          // You may add extra include path 
          // as of -I<path> option 
          //compilerArgs.add '-I/usr/local/include/foo'
          
          // ... or even as list of extra paths
          includes {
              '/usr/local/include/foo'
          }
      
          // Define toolchain-specific compiler options
          compilerArgs.addAll toolChain.map { toolChain ->
              if (toolChain in [ Gcc, Clang ]) {
                  return ['-O2', '-fno-access-control']
              } else if (toolChain in VisualCpp) {
                  return ['/Zi']
              }
              return []
          }
      }
      
      // Linker configuration
      tasks.withType(LinkExecutable).configureEach {
          // Add verbose output to linker
          // usefull while errors
          linkerArgs.add '-v' 
        
          // Add additional libraries as simple as
          linkerArgs.add '-lm' 
          linkerArgs.add '-lfoo' 
      
      }
      

      【讨论】:

        【解决方案4】:

        使用 Gradle 7.4.1,这适用于我的 cpp 库:

        apply plugin: 'cpp-library'
        
        ...
        
        library {
        
            ...
        
            binaries.configureEach {
                ...
                linkTask.get().linkerArgs.add("<argument here>")
                ...
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-11
          • 2011-07-23
          • 2015-01-05
          • 2015-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多