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

Started by
3 comments, last by Endurion 7 years, 7 months ago

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 -> Additional Dependencies I add "projects/foo/bin/x64/Debug/libfoo.so.1.0".

When I build and run the application I get an error inside the linux terminal that says


"error while loading shared libraries: projects/foo/bin/x64/Debug/libfoo.so.1.0. 
"cannot open shared object file name: no such file or directory"

I can't figure this out and google is no help.

Advertisement
Your linker input has "projects" in it, while the resulting path for the library doesn't. The paths don't match.

Simple check: Does a file "projects/foo/bin/x64/Debug/libfoo.so.1.0" exist where you expect it?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

More specifically, is the shared object at the specified path relative to the current working directory?

blah :)

alright, I added "./" before "projects" and now it works fine. my bad.

so now the path looks like this "./projects/foo/bin/x64/Debug/libfoo.so.1.0".

So now that everything works like it should, can someone explain what are these two warnings mean? and is there a way to fix them?


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).
Well, did you read the right part of the warnings?

They say exactly what the cause is; that the mismatch might cause problems later on; and the second part of the message starts with "to correct this...".
Did you check those properties if they do match %(Link.OutputFile)?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement