为Maven NAR项目的链接阶段添加静态库

时间:2015-12-03 21:38:57

标签: c++ maven opencv maven-plugin

我已将已编译的OpenCV版本打包到NAR Maven项目中。我已经跟着https://github.com/CERN/narlibs-log4cplus来实现这个目标(一个只包含.lib,.dll和.h文件的项目)。这是我的最终目录结构(lib目录包含.lib和.dll文件,目录include包含.h文件):

NAR packaged version of OpenCV

pom.xml文件:

...
<groupId>org.opencv</groupId>
<artifactId>opencv-nar</artifactId>
<packaging>nar</packaging>
<version>2.4.10</version>
<build>
    <plugins>
        <plugin>
            <groupId>com.github.maven-nar</groupId>
            <artifactId>nar-maven-plugin</artifactId>
            <version>3.0.0</version>
            <extensions>true</extensions>
            <configuration>
                <libraries>
                    <library>
                        <type>static</type>
                    </library>
                    <library>
                        <type>shared</type>
                    </library>
                </libraries>
            </configuration>
        </plugin>
    </plugins>
</build>

我在一个简单的项目中使用这种依赖:

#include "opencv2/highgui/highgui.hpp"
int main(int argc, char *argv[]) {
    cv::waitKey(0);
    return 0;
}

问题出现在链接阶段(执行mvn compile -X之后)。

...
[DEBUG] FileSet: Setup scanner in dir E:\PROY\image-lib\dev\1.0\image\target\nar\opencv-nar-2.4.10-x86-Windows-msvc-shared\lib\x86-Windows-msvc\shared with patternSet{ includes: [opencv-nar-2.4.10.lib] excludes: [] }
...
[DEBUG] Execute:Java13CommandLauncher: Executing 'link' with arguments:
'/MANIFEST'
'/NOLOGO'
'/SUBSYSTEM:CONSOLE'
'/INCREMENTAL:NO'
'/OUT:image.exe'
'C:\image\target\nar\obj\x86-Windows-msvc\image-test.obj'
[INFO] image-test.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _main
[ERROR] image-test.obj : error LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _main
[INFO] image.exe : fatal error LNK1120: 1 unresolved externals
[ERROR] image.exe : fatal error LNK1120: 1 unresolved externals
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

我看到的问题是因为OpenCv的静态库都没有传递给&#39;链接&#39;参数。那么,如何在link参数中添加静态库(或更好的目录)?

opencv_core2410.lib
opencv_imgproc2410.lib
opencv_highgui2410.lib

1 个答案:

答案 0 :(得分:0)

这不是一个笨蛋,而是更多的方法。我相信通过MAVEN NAR的设计(即使是MAVEN本身)每个库都必须在自己的项目中。

因此,考虑到这一点,您需要基于这两个项目生成项目:

因此,对于我的OpenCV项目,对于parent pom.xml from Boost C++ libraries in maven form,这是基于list of compiler names for the NAR plugin see in aol.properties的父pom.xml

<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>org.opencv</groupId>
    <artifactId>parent</artifactId>
    <version>2.4.10</version>
    <packaging>pom</packaging>

    <name>OpenCV :: Parent</name>
    <description>OpenCV</description>

    <properties>
        <nar-plugin.version>3.2.3</nar-plugin.version>
        <compiler-name>msvc</compiler-name>
        <linker-name>msvc</linker-name>
    </properties>

    <modules>
        <module>core</module>
        <module>highgui</module>
        <module>imgproc</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>${nar-plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <cpp>
                        <name>${compiler-name}</name>
                        <includes>
                            <include>**/*.hpp</include>
                            <include>**/*.h</include>
                            <include>**/*.cpp</include>
                            <include>**/*.c</include>
                        </includes>
                    </cpp>
                    <linker>
                        <name>${linker-name}</name>
                    </linker>
                    <libraries>
                        <library>
                            <type>static</type>
                        </library>
                        <library>
                            <type>shared</type>
                        </library>
                    </libraries>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

每个模块都有一个pom.xml文件,或多或少类似于:

<?xml version = '1.0'?>
<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.opencv</groupId>
        <artifactId>parent</artifactId>
        <version>2.4.10</version>
    </parent>

    <artifactId>opencv-core</artifactId>
    <packaging>nar</packaging>
    <name>OpenCV :: Core</name>
    <description>OpenCV</description>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

现在,您必须在项目pom.xml中添加相应的依赖项:

<dependencies>
    <dependency>
        <groupId>org.opencv</groupId>
        <artifactId>opencv-core</artifactId>
        <version>2.4.10</version>
        <type>nar</type>
    </dependency>

    <dependency>
        <groupId>org.opencv</groupId>
        <artifactId>opencv-highgui</artifactId>
        <version>2.4.10</version>
        <type>nar</type>
    </dependency>

    <dependency>
        <groupId>org.opencv</groupId>
        <artifactId>opencv-imgproc</artifactId>
        <version>2.4.10</version>
        <type>nar</type>
    </dependency>
</dependencies>

你有:

> mvn compile
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...