2016-09-18 42 views
0

我需要使用Visual Studio Linux Development Extension爲Linux創建一個動態庫的幫助。我從來沒有開發過Linux,但我做了我的研究,我被卡住了。我無法弄清楚什麼是錯的。在linux中創建一個動態庫並使用Visual Studio Linux Development鏈接到它

我正在使用此擴展。 https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/

這就是我所做的。首先我在Visual Studio中創建了一個名爲foo的空linux項目。我添加了一個名爲foo.h和foo.cpp的類。

foo.h中

#ifndef foo_h__ 
#define foo_h__ 

extern void foo(void); 

#endif 

Foo.cpp中

#include <stdio.h> 

void foo(void) 
{ 
    puts("Hello, I'm a shared library"); 
} 

在配置屬性我改變配置型從應用程序(.OUT),以動態庫(的.so)

在C/C++ - >命令行 - >我添加了「-fpic」。

我保存了,我建立了這個項目。

這是我的輸出

1>------ Rebuild All started: Project: foo, Configuration: Debug x64 ------ 
1> Cleaning remote project directory 
1> Validating architecture 
1> Validating sources 
1> Copying sources remotely 
1> Starting remote build 
1> Compiling sources: 
1> foo.cpp 
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(1190,5): warning MSB8012: TargetExt(.so.1.0) does not match the Linker's OutputFile property value (.0). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile). 
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(1191,5): warning MSB8012: TargetName(libfoo) does not match the Linker's OutputFile property value (libfoo.so.1). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile). 
1> Linking objects 
1> foo.vcxproj -> C:\Users\Fantasy-Desk\Desktop\test\foo\foo\bin\x64\Debug\libfoo.so.1.0 
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ========== 

之後,我創建具有的main.cpp文件在Linux控制檯應用程序。

#include <stdio.h> 
#include "../foo/foo.h" 

int main(void) 
{ 
    puts("This is a shared library test..."); 
    foo(); 
    return 0; 
} 

在配置屬性 - >鏈接器 - >輸入 - >庫依賴我添加「foo」和在附加依賴添加我的「libfoo.so.1.0」目錄在我的Linux機器,這是「項目/富/斌/ X64 /調試」

當我建立失敗的項目,我得到這個輸出

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug x64 ------ 
1> Validating architecture 
1> Validating sources 
1> Copying sources remotely 
1> Starting remote build 
1> Compiling sources: 
1> main.cpp 
1> Linking objects 
1>/usr/bin/ld : error : File format not recognized 
1>collect2 : error : ld returned 1 exit status 
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ========== 

我不知道這一點與谷歌是沒有幫助。有人可以告訴我什麼是錯的,爲什麼我不能編譯和鏈接並運行我的應用程序?

感謝

回答

0

在「附加依賴」,你需要指定實際庫文件名,而不是目錄。

+0

當我將libfoo.so.1.0添加到其他依賴項時,輸出文件中出現「1> g ++:error:No such file or directory」錯誤,但是當我添加「projects/foo/bin/x64/Debug/libfoo.so.1.0「出現錯誤,提示」1> collect2:error:ld returned 1 exit status「 – user2204292

+0

將此項添加到附加依賴項」projects/foo/bin/x64/Debug/libfoo.so .1.0「並從庫依賴關係中刪除」foo「,我的應用程序將編譯並獲取.out文件。但是,當我運行它時,我得到一個錯誤,說:「加載共享庫時出錯:項目/ foo/bin/x64/Debug/libfoo.so.1.0」無法打開共享對象文件名:沒有這樣的文件或目錄「 – user2204292

相關問題