我需要有关使用 Visual Studio Linux 开发扩展为 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.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");
}
在配置属性中,我将配置类型从 Application(.out) 更改为 Dynamic Library(.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 ==========
之后,我创建了一个 Linux 控制台应用程序,其中包含一个 main.cpp 文件。
#include <stdio.h>
#include "../foo/foo.h"
int main(void)
{
puts("This is a shared library test...");
foo();
return 0;
}
在 Configuration Properties -> Linker -> Input -> Library Dependencies 我添加“foo”,在 Additional Dependencies 我在我的 linux 机器中添加我的“libfoo.so.1.0”目录,即“projects/foo/bin/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 ==========
我想不通,谷歌也帮不上忙。谁能告诉我哪里出了问题,为什么我不能编译、链接和运行我的应用程序?
谢谢
最佳答案
在“附加依赖项”中,您需要指定实际的库文件名,而不是目录。
关于c++ - 在 Linux 中创建动态库并使用 Visual Studio Linux Development 链接到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560834/