在linux中创建一个动态库,并使用Visual Studio linux Development链接到它



我需要使用Visual Studio linux Development Extension为linux创建动态库的帮助。我从来没有为linux开发过,但我做了研究,我被困住了。我不知道出了什么问题。

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

这就是我所做的。首先,我在visual studio中创建了一个名为foo的空linux项目。我添加了一个名为foo.h和foo.cpp的类。

foo。

#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");
}

在配置属性中,我将配置类型从Application(.out)更改为Dynamic Library(.so)

C/c++ ->命令行->添加- fpic。

I saved and I build the project.

这是我的输出

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)MSBuildMicrosoft.Cppv4.0V140Microsoft.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)MSBuildMicrosoft.Cppv4.0V140Microsoft.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:UsersFantasy-DeskDesktoptestfoofoobinx64Debuglibfoo.so.1.0
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

之后,我创建了一个Linux控制台应用程序,它有一个main.cpp文件。

#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机器中是"projects/foo/bin/x64/Debug"

当我构建项目失败时,我得到这个输出

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 ==========

我想不明白,谷歌也帮不上忙。有人能告诉我什么是错的,为什么我不能编译和链接,并运行我的应用程序?

谢谢

在"Additional Dependencies"中,您需要指定实际的库文件名,而不是目录。

最新更新