Welcome! Log In Create A New Profile

Advanced

[RESOLVED] Error when using library

Posted by arasium 
[RESOLVED] Error when using library
October 14, 2009 12:02PM
Hi everybody,

i've code a quite large framework and it works very well. So i've decided to put it in a static library (.a) using that:

#---------------------------------------------------------------------------------
%.a:
	@echo linking to lib ... $(notdir $@)
	@$(AR) -rc $@ $^

# and i've changed the output with this
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).a: $(OFILES)

My library is well created (no errors or warning). I've recoded my application replacing all my framework source files by the library. It compile without any errors but it fails at runtime.
I haven't change any code in the source code except i've made a mylib.h wich include all elements of all my framework headers (i haven't put all method/function in mylib.h, because some are private methods and other are for a internal used).

Is someone has an idea about the origin of the bug? If i use the --whole-archive linker option will change something?
I'm lost :) I don't understand at all where this bug can come from.......



Edited 1 time(s). Last edit at 10/17/2009 02:30PM by arasium.
Re: Error when using library
October 14, 2009 03:26PM
What was the problem again?
Re: Error when using library
October 14, 2009 05:03PM
Sounds like you're not properly linking your library into projects that use it.
Re: Error when using library
October 14, 2009 05:32PM
Quote
Arikado
Sounds like you're not properly linking your library into projects that use it.

Yes, i was thinking about something like that. For the linker, i use the default devkitpro options. Should i add an other one?

Quote
Arikado
Sounds like you're not properly linking your library into projects that use it.
It depends :) Sometime many instance of a class are not well instanciated (instead, all instance are one. for exemple, if i create three counter, all counter will be at the same memory space....). Other time, the stack is corrupted or my object attribute values randomly change ?!!! So, i can say that it's a random effect.

I can't publish my code for the moment (i want to clean it, add comments etc....). So if you absolutely need it to help me, i will try to put the comments as soon as possible.
Re: Error when using library
October 14, 2009 06:21PM
Quote
arasium
Quote
Arikado
Sounds like you're not properly linking your library into projects that use it.

Yes, i was thinking about something like that. For the linker, i use the default devkitpro options. Should i add an other one?
Just several quick pointers:
1) Make sure that your lib is named libwhatever.a - You must have the first three characters named lib for it to be recogonized by devkitPPC compile

2) Make sure that your library is stored in the correct libs directory with the rest of your devkitPPC libraries (c:/devkitPRO/devkitPPC/libs -- Or something like that is the proper path)

3) In your makefile for your project using your library, link it in under LIBS: using "-l" in place of "lib" EX:
-lsomething

4) In your project's C/C++ files using your lib, make sure you properly #include it.

Good luck!!!



Edited 1 time(s). Last edit at 10/14/2009 06:22PM by Arikado.
Re: Error when using library
October 15, 2009 10:20AM
Quote
Arikado
Quote
arasium
Quote
Arikado
Sounds like you're not properly linking your library into projects that use it.

Yes, i was thinking about something like that. For the linker, i use the default devkitpro options. Should i add an other one?
Just several quick pointers:
1) Make sure that your lib is named libwhatever.a - You must have the first three characters named lib for it to be recogonized by devkitPPC compile

2) Make sure that your library is stored in the correct libs directory with the rest of your devkitPPC libraries (c:/devkitPRO/devkitPPC/libs -- Or something like that is the proper path)

3) In your makefile for your project using your library, link it in under LIBS: using "-l" in place of "lib" EX:
-lsomething

4) In your project's C/C++ files using your lib, make sure you properly #include it.

Good luck!!!

:) All of these points will lead to a compilation error. I haven't any compilation error, it's a runtime error.
Re: Error when using library
October 15, 2009 04:12PM
Are you mixing C and C++ code?
Re: Error when using library
October 15, 2009 04:55PM
Quote
arasium
it's a runtime error.
In that case, you need to show us your library and it's entire source or fix the problem yourself.
Re: Error when using library
October 16, 2009 11:10AM
Hi all, i've fixed the problem. I don't know if i have the good explanation but i willput it here :)

I imagine that i've a lot of classes (some are very simple and other aren't. For exemple, i use functor, the singleton pattern etc...). So, i've some private fields. All is compiling very well.

Then i create a library "libitems.a". I wan't to publish a libitems.h file wich will describe all my items. But this header won't contains all the field/methods of my classes (for exemple, i hide private elements). It won't succed at the runtime.
But if i put all elements in the header, it will run without any problem.

So, i think that when i compile my sources into a library, for:

class A
{
private:
  int test;
public:
  void Do();
}

The object in the library will only be registered under the class name. So the Do methods will be stored at the A object location plus an offset of 4 bytes (the length of hte private int field).

Then, if in my project i use the libitem.h like that:

class A
{
public:
  void Do();
}

and that code:

int main()
{
  A a;
  a.Do();
}

The compiler will create an object (fior the main function) and will put a link (for the line a.Do()) to the A object at the offset 0 (because in my libutils.h, i've not the int field).
Then the linker will try to find the A object (it will succed) and link to the offset 0 and this will create an error at the runtime because my method is at the offset 4. To add, it won't create an error at the compilation, because the linker only search the location of the A object, not the inner methods....

If i add the private int field into the libutils.h, all will run without any problem.

So here is my explanation. I don't know if these is the good explanation, but it can explain my problem.
Re: Error when using library
October 18, 2009 12:25AM
Yes, you can expect trouble if you use a different class definition like that.
Sorry, only registered users may post in this forum.

Click here to login