// this would typically be in another compilation unit
// and the header be included in the main program
class TextEditor
{
//...
public:
//...
void LoadFile(const char* name)
{
//...
};
char * FindText(const char* search)
{
return (char*) 0; // ok, there are better search algorithms ;-)
};
};
// the main program goes here...
#include <windows.h>
int main()
{
TextEditor t;
t.LoadFile("foo.txt");
char *c = t.FindText("some text"); // oh surprise... a missing symbol
return 0;
}
You will not be able to build this because your class has an unresolved symbol. Why is there an unresolved symbol, you wonder. Everything is correct, isn't it?
Yes, except that some freaking moron at Microsoft deemed it a good idea to implement Unicode support by defining a macro for each and every Win32 API function name (such as for example FindText).
Thus, if you include "windows.h" after including your own header, your member function is renamed to FindTextA or FindTextU, depending on whether you compile with Unicode support or without!
You can spend weeks searching for errors in your code where there are none, and you will never find out why it does not work. Aren't macros just great?
I see that you cannot mix C static libs with c++ ones!Actually, you can. Any decent C++ linker will allow you to interface with C object code, as long as your declarations are marked as C (using the extern "C" construct).
I see that you cannot mix C static libs with c++ ones!Actually, you can. Any decent C++ linker will allow you to interface with C object code, as long as your declarations are marked as C (using the extern "C" construct).
It's not Code::Blocks' fault, nor the toolkit's fault, just your own inexperience in doing things like this. Basically, if you want to use external (i.e. library) C functions and variables in your C++ code, you must (as with any library) declare the functions and variables for the C++ code, and you must surround the declarations with extern "C" { ... }.The problem had nothing to do with extern "C", though. ;)
It's not Code::Blocks' fault, nor the toolkit's fault, just your own inexperience in doing things like this. Basically, if you want to use external (i.e. library) C functions and variables in your C++ code, you must (as with any library) declare the functions and variables for the C++ code, and you must surround the declarations with extern "C" { ... }. Since the majority of C libraries have a header that you include to use the library, it's simplest to simply surround the #include directive with the extern "C" { } construct.