简体   繁体   中英

Create a dynamic library in linux and link to it using Visual Studio Linux Development

I need help with creating a dynamic library for linux using Visual Studio Linux Development Extension. I have never developed for linux but I did my research and I'm stuck. I can't figure out what is wrong.

I'm using this extension. https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/

So here is what I did. first I created an empty linux project in visual studio called foo. I added a class called foo.h and 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");
}

in the Configuration Properties I changed the Configuration type from Application(.out) to Dynamic Library(.so)

in C/C++ -> Command Line -> I added "-fpic".

I saved and I built the project.

This is my output

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

After that I created a Linux Console Application which has a main.cpp file.

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

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

In the Configuration Properties -> Linker -> Input -> Library Dependencies I add "foo" and in Additional Dependencies I add my "libfoo.so.1.0" directory in my linux machine which is "projects/foo/bin/x64/Debug"

When I build the project it fails and I get this output

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

I can't figure this out and google is no help. Can someone please tell me what is wrong and why can't I compile and link and run my application?

Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM