How to create a c/c++ shared library in VS2019

Anyone know if there is a sample out there on how to create a shared library using VS2019 extension or manually using cmake?
and then create another c++ app to call it.
after c++ app works, then I like to use c#(.net core) to call this library.

Shared Library function:

int getval()
{
       return 123;
}

Main testapp.cpp:

extern int getval();
int main()
{
    int testval;
    testval = getval();
    printf("testval = %d!\n", testval);
    return 0;
}

Greetings @swc,

Well this isn’t a use-case we’ve tried before but it should be somewhat possible. I asked our IDE experts about this and this is what they’d suggest:

  • Create a project using the library code sources as an initial base.
  • Add the application binary (assuming it was already compiled in a different project) to it and deploy together with the library.
  • Set exename property so it points to the application binary that should be started.

It’s a little not straight-forward but it seems to be the best way possible at the moment.

Best Regards,
Jeremias

unfortunately, I am a newbie to your platform.
I need a little bit more detail, how do you:
Create a project using the library code sources as an initial base.

I use the VS 2019 IDE, I only see way to create library, after I put in the source, no button is enabled to build it. so no exename anywhere.

What I mean by the first step is create a Torizon C/C++ application. This is the only project template that is supported by our extension. If you create any other project template our extension will not work.

In this project copy and paste your shared library code into the main code body. This is what I mean by creating a project where the library code is your base.

Then copy your application binary to the project. This is so that when the project is built and deployed as a container, both your shared library and the application that uses it gets deployed. However currently our extension will only build one thing at a time which is why we copy the application pre-compiled. I suppose you could go the other way and start with the application source and then copy the library binary into the project, but either way.

Then via the Torizon project properties there should be a propety called exename you want this to be your application binary. Otherwise by default it will try and execute your shared library binary which obviously wouldn’t be correct.

Also if you are new to our VS IDE extension I would highly suggest looking at the following article: https://developer.toradex.com/knowledge-base/c-and-c-workflow-example

This article shows how to use our extension on a decent sized project as well as do basic functions like changing the project properties of the extension and such.

Best Regards,
Jeremias

OK, that sounds weird. I’ll give it a try. because I was expecting creating a .so file.
now what I understand you’re trying to say is:

  1. create a project call TestApp, binary is TestApp.out that will call getval() in a ‘library’
  2. create a project call MyLib with a binary MyLib.out and with the function lib getval()
  3. in project create #2, I copy TestApp.out to project #2 where MyLib.out is. and set the exename to be TestApp.out

is that what you’re saying?
I only know calling a library in .a or .so, but never a .out

as expected, not working.

  1. linker have error since it doesn’t know about my library functions in testapp. I’m suppose to link this testapp to the shared library .so

just want to make sure you understand what I’m trying to do.
testapp calls a library function in mylib

testapp.cpp

#include <cstdio>
extern int getval();
int main()
{
    printf("hello from testapp\n");
    printf("%d\n", getval());
    return 0;
}

1>Compiling sources: 1>main.cpp
1>Linking objects
1>/usr/lib/gcc-cross/aarch64-linux-gnu/10/…/…/…/…/aarch64-linux-gnu/bin/ld
: error :
/home/build/testapp/obj/x64/Debug_debian_arm64v8_bullseye/main.o:
in function main': 1>C:\VulcanEmbedded\testapp\main.cpp(9): error : undefined reference to getval()’ 1>collect2 : error : ld
returned 1 exit status 1>Done building
project “testapp.vcxproj” – FAILED.

and I can’t findd anything in the project setting to set the .so
also, we really don’t have a .so, because mylib project only have .out
and in mylib project, I can’t found the exename you refer to, I see program under debug

mylib.cpp in mylib project:

#include <cstdio>
    int getval()
    {
        return 123;
    }
    int main()
    {
        printf("hello from mylib!\n");
        return 0;
    }

Issue was discussed more in-depth in an external call.

But for the rest of the community here. It was decided that this is probably easier to do in the VS Code extension as the process is more readily customizable there.

Inspiration for what could be done can be taken from our example here: https://developer.toradex.com/knowledge-base/torizon-cc-application-importing-example

In the above example we modify the default build steps to build a library that the main application relies on. Something similar can be done for other use-cases as well.

Though eventually we hope to have a simpler example of this that can be generally applied.

Best Regards,
Jeremias