首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在eclipse中使用Maven构建c++

在eclipse中使用Maven构建c++
EN

Stack Overflow用户
提问于 2012-12-01 21:41:58
回答 1查看 2.9K关注 0票数 18

我的项目由多个Java项目组成,一个作为桥梁的Java-JNI-C++项目和一个保持算法库的纯C++项目。我设法为所有3种项目类型编写了Maven构建配置。所以当我在命令行(Windows7,64位)上调用它们时,一切都很好。

我不使用任何make文件或类似的东西。我使用exec-maven-plugin在没有cygwin的情况下调用我的mingw64bit安装(至少我也没有故意安装msys)。因此,两个纯命令行g++命令分别用于JNA和库项目。

我现在需要的是能够在Eclipse中使用maven构建脚本来构建和调试this项目,因为我不想将工作放到pom中并额外配置eclipse构建器。这应该是一致的!此外,Eclipse中的错误解析应该与maven构建的输出一致。

对于我的Java项目,这是开箱即用的。Eclipse采用maven配置,并进行清理和构建,生成它应该生成的内容。(尽管我看到Java Builder在项目的属性中仍然处于活动状态。为什么??)。但我不能让它和CDT一起工作。

当我禁用 Builder时,C++只是用maven构建(我想要的),但是clean命令不能正常工作。我还得到了错误标记,这些错误不是编译器标记的错误。当然,这应该是一致的。

有关于这个用例的教程吗?

我没有找到关于那个主题的信息。我不确定我是不是走错了方向,缺少最佳实践还是什么?!

由于这是我的第一个s.o.问题,请随时就我的问题给我反馈。我能提供的我会提供;-)

一些信息:

系统Windows 7,64位

Eclipse Juno,m2e

库POM:

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>mylib</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>MyLib</name>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <versionRange>[1.1.1,)</versionRange>
                    <goals>
                      <goal>exec</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute>
                      <runOnIncremental>true</runOnIncremental>
                    </execute>
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1.1</version>
        <executions>
          <execution>
            <id>compile-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target/cpp/Windows_x64</workingDirectory>
              <arguments>
                <argument>-Wall</argument>
                <argument>-m64</argument>
                <argument>-c</argument>
                <argument>-DAPI_EXPORT</argument>
                <argument>-g3</argument>
                <argument>-std=c++0x</argument>
                <argument>../../../src/main/cpp/*.cpp</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>link-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target</workingDirectory>
              <arguments>
                <argument>-shared</argument>
                <argument>-s</argument>
                <argument>-m64</argument>
                <argument>-oMyLib_Windows_x64.dll</argument>
                <argument>cpp/Windows_x64/*.o</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>none</phase>
          </execution>
        </executions>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

JNI POM:

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>myprog</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>MyProg</name>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>truezip-maven-plugin</artifactId>
                    <versionRange>[1.1,)</versionRange>
                    <goals>
                      <goal>copy</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute>
                      <runOnIncremental>true</runOnIncremental>
                    </execute>
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <versionRange>[1.1.1,)</versionRange>
                    <goals>
                      <goal>exec</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute>
                      <runOnIncremental>true</runOnIncremental>
                    </execute>
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>truezip-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>get-library-headers</id>
            <goals>
              <goal>copy</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <fileset>
                <directory>../MyLib/target/mylib-0.0.1-SNAPSHOT-dll.zip</directory>
                <includes>
                  <include>headers/*</include>
                </includes>
                <outputDirectory>${project.build.directory}/myLib</outputDirectory>
              </fileset>
            </configuration>
          </execution>
          <execution>
            <id>get-library</id>
            <goals>
              <goal>copy</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <fileset>
                <directory>../MyLib/target/mylib-0.0.1-SNAPSHOT-dll.zip</directory>
                <includes>
                  <include>*.dll</include>
                </includes>
                <outputDirectory>${project.build.directory}</outputDirectory>
              </fileset>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1.1</version>
        <executions>
          <execution>
            <id>compile-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target/cpp/Windows_x64</workingDirectory>
              <arguments>
                <argument>-Wall</argument>
                <argument>-m64</argument>
                <argument>-c</argument>
                <argument>-g3</argument>
                <argument>-std=c++0x</argument>
                <argument>-I../../myLib/headers</argument>
                <argument>../../../src/main/cpp/*.cpp</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>link-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target</workingDirectory>
              <arguments>
                <argument>-m64</argument>
                <argument>-s</argument>
                <argument>-oMyProg_Windows_x64.exe</argument>
                <argument>cpp/Windows_x64/*.o</argument>
                <argument>MyLib_Windows_x64.dll</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>none</phase>
          </execution>
        </executions>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-13 22:47:48

Eclipse不而不是使用Maven。相反,它会根据您的POM配置JDT。这是通过M2E ()连接器完成的。如果您希望同样适用于CDT,那么您需要一个M2E连接器。我不知道有没有这样的插件,所以你必须为此编写一个Eclipse插件。有关如何做到这一点的详细信息,请参阅http://wiki.eclipse.org/M2E/Extension_Development

当然,您也可以在pom的lifecycleMapping中使用"execute“而不是"ignore”。但这可能会导致构建速度相当慢,因为每次更改都会触发整个构建。尽管如此,这仍然不会“神奇地”使Maven中的错误出现在您的文件中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13660014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档