-
February 15th, 2010, 03:55 AM
#1
typedef and static libraries
I'm trying to create a static abstraction system, where I have a system library and an application.
The System Library has a function that takes a BufferObject * as a parameter - the header that it includes is an empty class declaration with no definitions.
In my application, a class such as BufferObject_D3D is created using typedef BufferObject_D3D BufferObject. The interface is the same as the empty declaration.
When I try using the function, I get an unresolved external, where there is no function that takes a BufferObject_D3D * as a type.
Is there any way that I can solve this?
-
February 15th, 2010, 05:02 AM
#2
Re: typedef and static libraries
we need to see the code.
unresolved external symbol usually occurs when the linker fails to find the definition.
since your static library has external linkage,make sure the class and the functions are fully defined prior to use.
-
February 15th, 2010, 06:49 AM
#3
Re: typedef and static libraries
Originally Posted by mkukli
The System Library has a function that takes a BufferObject * as a parameter - the header that it includes is an empty class declaration with no definitions.
In my application, a class such as BufferObject_D3D is created using typedef BufferObject_D3D BufferObject. The interface is the same as the empty declaration.
When I try using the function, I get an unresolved external, where there is no function that takes a BufferObject_D3D * as a type.
Is there any way that I can solve this?
A typedef is nothing more than an alias for a certain type. So when you do this
Code:
//...
typedef BufferObject BufferObject_D3D;
int main()
{
BufferObject_D3D foo;
bar(&foo);
}
The compiler will search for a function called bar that takes a pointer to a BufferObject (or something to which a BufferObject* is convertible).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 15th, 2010, 07:03 PM
#4
Re: typedef and static libraries
I can't show code for this, but I can give an overview.
There are two main compilations being done - a static library, and an app that is using it.
The define LIB is defined if it is the library being compiled.
In 'bufferobject.h', I'd have:
#ifndef LIB
# ifdef D3D
typedef BufferObject_D3D BufferObject;
# ...
# endif
#endif
As you can see, I am making it so ALL abstract version (BufferObject_D3D, BufferObject_GL, etc) are being compiled in to the library, and I am statically defining which it is by a typedef in the engine. Both classes have the same interface.
I am thinking that the only way I can solve this is to have functions that use this type to be in header form so they get reformed by the application? I am trying to avoid having to use an object factory for this abstract code.
-
February 15th, 2010, 11:43 PM
#5
Re: typedef and static libraries
You're basically doing this
Code:
#ifdef D3D
class BufferObject_D3D;
typedef BufferObject_D3D BufferObject;
#else
class BufferObject_GL;
typedef BufferObject_GL BufferObject;
#endif
void Func(BufferObject* ptr);
// above definitions lie some where in other translation unit
int main()
{
BufferObject_D3D d3d;
BufferObject_GL gl;
Func(&d3d); // bound to cause problems
Func(&gl); // same here
}
You're lucky that this results in a compile time error, had this static library been linked dynamically, you're in for the trouble at run time.
You're getting the error because somewhere in your main app, the compiler is trying to find the one that was excluded by the #define statement, e.g., if you defined D3D, any calls to BufferObject_GL will result in unresolved external symbol error. On the other hand, if you had not defined D3D, then the same thing happens for the BufferObject_D3D. This is a common problem in conditional compilation. I'd limit this type of approach for debugging or internal uses only.
I am thinking that the only way I can solve this is to have functions that use this type to be in header form so they get reformed by the application?
Forget about the static library. It's just a set of object files bundled into one which you could freely add and remove. Instead, focus first on the flow of your program and make it right. And no, you can't do that since the definition of any functions and class must be seen at the time of compilation if they're to be linked internally.There are number of ways you can work around it, e.g., function overloading or the template approach. Key thing here is that you avoid the inconsistency
I am trying to avoid having to use an object factory for this abstract code
If your static library servers no purpose other than a pure interface, you don't need to make it into a library, a simple header will suffice. And before you try to figure out how to avoid whatever it is that you're doing, design a solid object model which in turn would help you to design a better class hierarchy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|