Author Topic: Shared lib question  (Read 5105 times)

Offline CarstenT

  • Multiple posting newcomer
  • *
  • Posts: 11
Shared lib question
« on: January 22, 2015, 03:16:50 pm »

Hi all,
I've compiled the default 'shared library' projekt and a console test project where I link up with the shared library. The test-project compiles ok and no linker errors. From here I feel extremely stupid that I cannot access the functions in the library.

The shared library has something like
 
main()
{
int add( int a , int b ){ return a + b ;}
 .. more functs
}

it beats me why I get an error when I use add(..) in the Test-consol main()
Should I add some #include directive? Use a prefix. Create an object of sort and access add() as a member?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Shared lib question
« Reply #1 on: January 22, 2015, 09:27:46 pm »
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Brute

  • Single posting newcomer
  • *
  • Posts: 8
Re: Shared lib question
« Reply #2 on: January 22, 2015, 11:53:41 pm »
main()
{
int add( int a , int b ){ return a + b ;}
 .. more functs
}

I would recommend you to visit http://www.cplusplus.com/forum/beginner/
« Last Edit: January 22, 2015, 11:55:33 pm by Brute »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Shared lib question
« Reply #3 on: January 23, 2015, 12:07:15 am »
main()
{
int add( int a , int b ){ return a + b ;}
 .. more functs
}

I would recommend you to visit http://www.cplusplus.com/forum/beginner/

Please read the prior post on the full build log before going to another C++ site to ask for help.

You should also post the error message when you ask for help.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline CarstenT

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Shared lib question
« Reply #4 on: January 23, 2015, 01:47:01 pm »
hi folks,
thanks for the response.

I've added an
#include <main>
in the consol test project. That made the function add(..) visible. I havn't tested weather I can change the name 'main()' as I find it inappropriate as a name of a library.
As for going through the compile error of the consol test project compilation .. it seems as an error that I've met many times before and would contain something like 'function add(..)' not available in this scoop (and not how to mend the problem).
Anyway, since I've used only very simple default projects, I thought that you could be interested in the general problem. I mean, wouldn't add(..) be available in some standard manner if I use the standard project and standard linking?
I'm pretty sure that I started a shared library project .. for some reason I had to change the compile target later on from dynamic library to shared library. This could make something off-standard.

thanks for the attention
« Last Edit: January 23, 2015, 01:49:12 pm by CarstenT »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Shared lib question
« Reply #5 on: January 23, 2015, 07:20:27 pm »
Error message contain information useful to determine the cause of the problem.
Based on the little info you posted we must assume you are the problem!!
Without a build log or the real error message the best guess is you are the problem.

Please do not waste someone else's time by not posted the needed info.
If you wish to do that call a paid psychic help line.

We are NOT psychic to help you requires information that you need to provide!!

Edit: FYI, dynamic library means the same as shared library in nearly all cases. Static and Shared are NOT the same.
Linux tends to use the word shared while Windows uses DLL Dynamic Link Library http://en.wikipedia.org/wiki/Dynamic-link_library.

Tim S.
« Last Edit: January 23, 2015, 07:27:46 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline CarstenT

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Shared lib question
« Reply #6 on: January 24, 2015, 06:57:41 pm »

stahta01,

sorry for being so much of an inconvenience, but I'll not try to reconstruct the error alone for getting a proper file-report. I don't know why you don't understand

quote
I've added an
#include <main>
in the consol test project. That made the function add(..) visible
unquote

to be a solution to my problem (that I cannot access the functions in the library.)
I got the solution before I got your response. As for the shared/dynamic .. why would the pop-up menu give me both choices if there are no difference?

take a break.
You really remind me why I don't frequent these forums anymore

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Shared lib question
« Reply #7 on: January 24, 2015, 07:41:17 pm »
Sorry, but stahta01 is correct about the logs.
We need them, so we can get an idea what you are doing and what the real errors are!

The code you're pasting doesn't look like a valid c/c++, so how do  you expect us to help you?
You've created a header file named main, without extension?

Why it is so hard for you to cooperate?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline CarstenT

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Shared lib question
« Reply #8 on: January 25, 2015, 07:45:46 pm »

hi oBFusCATed,

Quote
Why it is so hard for you to cooperate?

I use this public pc outside my home to get on the net. My working-pc is at home. That does make communication somewhat slow and inconvenient as for just handing info over on request. The separation is a bliss for my general mood though.

My initial question is a bit unimportant since I've got something to work. I ought to have tried to gain more experience on the subject before asking in here. When I requested a 'shared library' I got  something very much like this

Code

// this dummy function /will/ be accessible in your new library

extern "C"
{
        main()
        {
                 int add( int a , int b )
                 {
                        return a + b ;
                  }
        }
}

After establishing a console project and linking to the library I could use 'add(..)' if I added
#include <main.h>
as any other directive I would use in a project.
And I bet you know the type of error-report you get if you take such a directive away, because we've all seen it so often.
I'm not kean on using
main
as the name of a library, but that's a minor inconvenience.
As for valid c/c++ it's much better than it used to be .. I've got 3-4000 pages to support me at home.

Code::Blocks is an impressive project

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Shared lib question
« Reply #9 on: January 25, 2015, 09:23:39 pm »
Sorry, but I still don't think the snippet you've posted compiles without errors.
If you post code snippets from memory then for sure you're messing the little details and those are the really important ones.

Can you just use a flash drive, cd or even a floppy disk :) to transfer the files to the connected pc, so you can provide accurate information, so we can try to help you.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline CarstenT

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Shared lib question
« Reply #10 on: February 19, 2015, 11:40:11 am »

oBFusCATed,

I've ditched the example I started out with. I probably erected the testConsole project in the same folder as the library I wanted to test - or changed target between shared/dynamic, and it does work.
But, I've started from scratch testing the static, shared and dynamic-dll example library-projects in associated console-test-projects. Only the shared library project will not work. The functions of the static library becomes available in the console if I
#include <main.c>     // which would be the source-file name of the static library
All the console test projects (that I've made) has one source-file, "main.cpp". The shared library has one source-file of the same name, and when I set the test-console up and include "main.cpp", the compilation goes into an infinit loop. That may not be odd, but how would I set your example projects up to work without this error?

I would like to be sure that I do it right because I have more ambitious projects in mind. I have opengl code working with all my hearts desire of functionallity .. in the context of a console project. I would like to make a dynamic or shared library of that functionallity. So far I've managed to put the vital initiations in one separate class (class.h & class.cpp in the console project), and the console can instantiate an object and run it. Compiling has not succeeded in one go, I've had to add functions to the class one by one to finally end up with all contained - so I'm not very confident that I can just move the class.h & class.cpp to some library and expect it to work. Best to have it in a shared library, so I tried the above.

I've read part of Peter Scmidt's book "Software build systems", but he speaks almost solely of dll's, not shared libraries. I'm not sure of the differences. Both code::blocks's examples produces .a and .dll output, but the contents of the examples are very different. I take that the dynDell looks odd because of the use of windows.h and all what follows.

Can I make you test the shared library project with a console-project to see if you get the same infinite loop, or suggest what I could be doing wrong?
I link to libSomeName, and let linker search in the library where it is
The only sourcefiles is in the same folder as the project, so I let the compiler search there. It could make sense that any of them might need the object-files, but I think that I've added search to those libs as well. We are talking about console execution from within code::blocks. I've made sure that execution outside needs the library in the same folder as the .exe.

I'll go home and try something more .. it usually works at the end (though I don't always knows why)

CarstenT

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Shared lib question
« Reply #11 on: February 19, 2015, 06:03:17 pm »
Please read the CB FAQs and the CB Rules!
http://forums.codeblocks.org/index.php/topic,9996.0.html

You need to learn how to post a full re-build log or I at least will ignore your questions.

FYI: Including an .c or .cpp file is normally the wrong thing to do in C or C++ programming.
Quote
#include <main.c>     // which would be the source-file name of the static library

Edit2: I suggest reading the user docs, also. http://wiki.codeblocks.org/index.php?title=User_documentation
http://wiki.codeblocks.org/index.php?title=FAQ

Tim S.

« Last Edit: February 19, 2015, 06:06:27 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline CarstenT

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Shared lib question
« Reply #12 on: February 20, 2015, 09:09:58 am »
hi Tim S,

Thanks for the links. And your time.