Looking for help with C++ using shared library. -Solved!

mihasic

New Member
Joined
Dec 10, 2022
Messages
3
Reaction score
1
Credits
49
Could somebody please help me out, I am confused with command line compilation (C++).
I created a dummy shared library with CodeBlocks and it work ok. Then I tried to use it in an equally dummy application, and that's there that I got my true problem. Shortly, the library is never and by no means seen. First I tried to reach it with CodeBlocks and it never worked in spite of all the attempts. So I decided to purify my experience and tried to link it from the command line.
I looked up the net and found some straightforward guides.
I started with this one
and it was like this:

# Create the executable by linking shared library
gcc -L<path to .SO file> -Wall -o code main.c -l<library name>
# Make shared library available at runtime
export LD_LIBRARY_PATH=<path to .SO file>:$LD_LIBRARY_PATH
# Run executable
./a.out

I never could pass across the very first line, it always explained me that there was no such file or directory.
I am rather new to linux, so I was not sure that I understood it right, so I played a little bit around. I tried to use or not to use angular brackets, to insert or to eliminate spaces before the path and the library name, to bring the library directly to the working directory and thus eliminate using path, to eliminate or not the 'lib' prefix and the library file '.so' extension (that was noticed at another guide, (https://www.ca5.co/code/linux/gcc/gcc-l). I always got the same, either 'cannot find -lMySharedLibrary', or 'MySharedLibrary: no such file or directory.

WHAT - AM - I - DOING - WRONG - ? Please...
 
Last edited:


First, did you actually run the steps before this from the link you posted to create thr .so file in the first place?
Second, what angle brackets are you talking about? Do you mean where it says <path to .SO file> or <library name>? Those shouldn't be used at all. You need to actually use the path and library name in place of what is listed.
Instead of re-posting what is in the article, it would help if you stated exactly what you're typing in and exactly what error you're getting.
 
You should probably ask this somewhere else: this isn't the ideal place for c++ related issues.

Plus, you're not posting ouput for your compilation commands: that's a necessary part of being able to figure out whatever you're trying to accomplish. Slow down and try to find c++ coders for answers. I haven't messed with c++ in years.
 
First, did you actually run the steps before this from the link you posted to create thr .so file in the first place?
Second, what angle brackets are you talking about? Do you mean where it says <path to .SO file> or <library name>? Those shouldn't be used at all. You need to actually use the path and library name in place of what is listed.
Instead of re-posting what is in the article, it would help if you stated exactly what you're typing in and exactly what error you're getting.
Fine. I still continue my research.
No, at the moment of my first post I did not create my library with the command line. As said in my post, the library had been created with CodeBlocks. But this doesn't seem to matter too much, as since then I tried another guide,
Unfortunately, it was very much alike. This time I tried to follow it precisely, but It produced errors in compilation so I had at least change the order of commands.
Here is what I did:
First, I manually recreated my library:
Here is the library text:
double Emul(double x1, double x2)
{
return x1 * x2;
}
double Ediv(double x1, double x2)
{
return x1 / x2;
}
I did
$ g++ -o NewLib.o -c main.cpp
$ g++ -shared NewLib.o -o libFunctionsE.so

Up to here it was ok.
Then I copied libFunctionsE.so file to the directory of the code that was supposed to use it.
The code in question contains two files, FunctionsE.h and main.cpp .

"FunctionsE.h"
extern "C" double Emul(double, double);
extern "C" double Ediv(double, double);

"main.cpp"
#include <iostream>
#include "FunctionsE.h"
using namespace std;
int main()
{
double U1 = 5, U2 = 4;
cout << Emul(U1, U2) << " " << Ediv(U1, U2)<< endl;
cout << "Hello world!" << endl;
return 0;
}

And there I did:
$ g++ -c main -o intermediate.o
it also passed well.
Then I tried to link the library:
$ g++ -o Vot -L. intermediate.o -lFunctionsE
I get error:
/usr/bin/ld: intermediate.o: in function 'main'
main.cpp; (.text+0x3a): undefined reference to 'Emul'
/usr/bin/ld: main.cpp: (.text+0x6f0: undefined reference to 'Ediv'
collect2: error: ld returned 1 exit status

It's like this. If you are still reading, I'm already grateful.
 
I haven't been using Linux for development a lot, but I think the ordering of arguments for these programs matter. I would start by trying to reorder the arguments you typed to match the order stated in the article you posted.

I'll look a little more and see if there's anything rise obvious
 
I looked a little closer and was able to get it working by removing 'extern "C" ' from your function declarations in your header file. Give that a shot.
 
Sorry I’m late to the party.
I only just saw this because I’d been tagged in a post here!

@CrazedNerd :
You should probably ask this somewhere else: this isn't the ideal place for c++ related issues.
Um….. No! I disagree. This is a perfect place to be asking. The OP is asking about a C++ development issue on Linux. And we are a Linux community.

We don’t have any dedicated programming forums here, but many programming questions like this end up in command line, or general Linux for issues with IDE’s.

Also, I’m a professional C++programmer, with a lot of experience with C and numerous other programming languages.
There are a few other regular and occasional members with development experience who can answer questions like this too.

@mihasic :
@MattWinter is 100% correct here.
In a C++ program, extern "C" should only be used if the library you’re loading/using was compiled as a C library using a C compiler (eg. gcc, or clang).

But you’ve compiled your library using g++, which is a C++ compiler. So your library is already a valid C++ library and therefore there is no need to be declaring the library functions as extern "C" in your C++ program.

So extern "C" allows C libraries to be loaded and used in C++ programs/libraries.
 
Um….. No! I disagree. This is a perfect place to be asking. The OP is asking about a C++ development issue on Linux. And we are a Linux community.
Although it's not my thing [programming] I have to agree with you, We are a broad church and all have our own specialities to bring to the forum, As we have members [like yourself] who can assist newbies on their path to programming then why not
 
Sorry I’m late to the party.
I only just saw this because I’d been tagged in a post here!

@CrazedNerd :

Um….. No! I disagree. This is a perfect place to be asking. The OP is asking about a C++ development issue on Linux. And we are a Linux community.

We don’t have any dedicated programming forums here, but many programming questions like this end up in command line, or general Linux for issues with IDE’s.

Also, I’m a professional C++programmer, with a lot of experience with C and numerous other programming languages.
There are a few other regular and occasional members with development experience who can answer questions like this too.

@mihasic :
@MattWinter is 100% correct here.
In a C++ program, extern "C" should only be used if the library you’re loading/using was compiled as a C library using a C compiler (eg. gcc, or clang).

But you’ve compiled your library using g++, which is a C++ compiler. So your library is already a valid C++ library and therefore there is no need to be declaring the library functions as extern "C" in your C++ program.

So extern "C" allows C libraries to be loaded and used in C++ programs/libraries.
My bad, i just tend to try to think like: "i have this problem, so ill go to a ______" forum, since this is a command line related thing then i guess its apprpriate.
 
We don’t have any dedicated programming forums here, but many programming questions like this end up in command line, or general Linux for issues with IDE’s.

I've actually pondered this and have considered approaching @Rob (or @wizardfromoz, though this looks like something he'd kick upstream) to ask if we could have a dedicated Programming section. While it can take place in the terminal, it doesn't have to. Some of what we move into the Command Line section would be better suited in a Programming-specific sub-forum.
 
Oh. I thought Dev-ops would be the place for this kind of stuff. Maybe not.

Dev-ops is more specific - developer operators, basically the business world's way of squeezing pennies a bit harder.
 
I looked a little closer and was able to get it working by removing 'extern "C" ' from your function declarations in your header file. Give that a shot.
You know, MattWinter, you are right. I threw away extern "C" and it worked very well. The problem is resolved. Thanks a lot. Thanks to all you guys who took part in the discussion.
 
Thanks to all you guys who took part in the discussion.
If you're now happy. Please go back to your first post and add "solved" to the title
 

Members online


Latest posts

Top