【问题标题】:Create a dynamic library in linux and link to it using Visual Studio Linux Development在 linux 中创建一个动态库并使用 Visual Studio Linux Development 链接到它
【发布时间】:2016-09-18 17:51:35
【问题描述】:

我需要有关使用 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 ==========

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

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

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

在配置属性 -> 链接器 -> 输入 -> 库依赖项中,我添加了“foo”,在附加依赖项中,我在我的 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++ c linux visual-studio-2015


    【解决方案1】:

    在“附加依赖项”中,您需要指定实际的库文件名,而不是目录。

    【讨论】:

    • 当我将 libfoo.so.1.0 添加到附加依赖项时,我在输出文件中收到一个错误,显示“1>g++:错误:没有这样的文件或目录”但是当我添加“projects/foo /bin/x64/Debug/libfoo.so.1.0”我收到一条错误消息“1>collect2:错误:ld 返回 1 退出状态”
    • 好吧,在将它添加到附加依赖项“projects/foo/bin/x64/Debug/libfoo.so.1.0”并从库依赖项中删除“foo”之后,我的应用程序编译并得到了 .出文件。但是,当我运行它时,我收到一条错误消息,提示“加载共享库时出错:projects/foo/bin/x64/Debug/libfoo.so.1.0。”无法打开共享对象文件名:没有这样的文件或目录”跨度>
    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多