2011-02-08 15 views
1

我正在嘗試使用Maven NAR插件構建一個非常簡單的C++程序。我建立了一個用於構建共享庫的Maven模塊,另一個用於鏈接庫並構建使用它的可執行文件。建立在Mac上的工作很棒,我可以運行該程序。不幸的是,使用MS Visual C++(免費版本)在Windows(XP)上構建失敗,並顯示鏈接器錯誤。兩臺機器(操作系統和編譯器除外)之間配置唯一的區別在於,我在Windows機器上使用Maven構建之前運行vcvars32.bat。下面是我收到的錯誤:使用Maven NAR插件與Windows上的DLL鏈接

main.obj : error LNK2019: unresolved external symbol "public: int __thiscall 
Calculator::add(int,int)" ([email protected]@@[email protected]) referenced in function 
_main executable.exe : fatal error LNK1120: 1 unresolved externals 

鏈接器命令吐出由NAR插件看起來是這樣的:

link /MANIFEST /NOLOGO /SUBSYSTEM:CONSOLE /INCREMENTAL:NO /OUT:executable.exe 
C:\dev\Projects\trunk\executable\target\nar\obj\x86-Windows-msvc\main.obj 

我希望它應該有我的共享庫模塊生成列出的DLL,但它不在那裏。它的NAR解壓縮到可執行文件的目標目錄中,就像它應該一樣。

任何幫助配置Windows的NAR插件將不勝感激。或者,顯示如何正確執行鏈接程序的命令行將非常有用,因此我可以回填NAR配置以實現它。謝謝。

我的共享庫模塊:

Calculator.h

#ifndef CALCULATOR_H 
#define CALCULATOR_H 

class Calculator { 
public: 
    int add(int first, int second); 
}; 

#endif 

Calculator.cc

#include "Calculator.h" 

int Calculator::add(int first, int second) { 
    return first + second; 
} 

的pom.xml(片段):

<groupId>com.mycompany</groupId> 
<artifactId>library</artifactId> 
<version>1.0.0-SNAPSHOT</version> 
<packaging>nar</packaging> 

... 

<plugin> 
    <artifactId>maven-nar-plugin</artifactId> 
    <version>2.1-SNAPSHOT</version> 
    <extensions>true</extensions> 
    <configuration> 
     <libraries> 
      <library> 
       <type>shared</type> 
      </library> 
     </libraries> 
    </configuration> 
</plugin> 

我的可執行模塊:

main.cc

#include <iostream> 
#include "Calculator.h" 

int main() { 
    Calculator calculator; 
    std::cout << calculator.add(2, 5) << std::endl; 
} 

的pom.xml(片段)

<groupId>com.mycompany</groupId> 
<artifactId>executable</artifactId> 
<version>1.0.0-SNAPSHOT</version> 
<packaging>nar</packaging> 

<dependency> 
    <groupId>com.mycompany</groupId> 
    <artifactId>library</artifactId> 
    <version>1.0.0-SNAPSHOT</version> 
    <type>nar</type> 
</dependency> 

... 

<plugin> 
    <artifactId>maven-nar-plugin</artifactId> 
    <version>2.1-SNAPSHOT</version> 
    <extensions>true</extensions> 
    <configuration> 
     <libraries> 
      <library> 
       <type>executable</type> 
      </library> 
     </libraries> 
    </configuration> 
</plugin> 

回答

2

回答我的問題。

我的一個同事挖進了他大腦的陰暗的凹處,並說他回想起需要「dicklespeck」的東西。這聽起來很奇怪,所以我把它放在「如果一切都失敗了,我會看看」桶。在所有其他的失敗之後,我回到了Google,並用Google搜索了各種拼寫,顯示他是正確的。如果我將這種憎惡添加到我的類聲明中:

__declspec(dllexport) 

DLL成功鏈接到可執行文件。

因此, 「固定」 計算器頭文件,像這樣是解決方案:

#ifndef CALCULATOR_H 
#define CALCULATOR_H 

class __declspec(dllexport) Calculator { 
public: 
    int add(int first, int second); 
}; 

#endif 

呸!我可以用#define這個東西遠離非windows的版本,但仍然 - 惡作劇!

有人請告訴我,這不是唯一的解決方案。

+0

這在窗口中很常見,您必須標記要導出的項目。有出口和進口的變化(或者對於靜態鏈接),這裏是合理的開始SO上http://stackoverflow.com/questions/4983835/export-dlls-classes-and-functions-and-import-them-into- win32-application – 2011-05-05 04:25:08

相關問題