Author Topic: bool in static libs?  (Read 7969 times)

rassilon

  • Guest
bool in static libs?
« on: March 23, 2006, 05:38:49 pm »
Am I supposed to get a compile error:

missing ')' before identifier

when using a bool in a function:

void testSpot(bool t);

compiling with visual toolkit 2003 and SDK 2003?

I know that when I compile a console app etc I dont get these issues...Also if I change to int it passes...

I guess Im missing a header but for the life of me I cannot remember what header contains bool...

Thanks in advance...

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: bool in static libs?
« Reply #1 on: March 23, 2006, 05:51:53 pm »
bool is a C++ keyword, so you don't need any headers for it.

However, Microsoft is notorious about using CUMs (Completely Useless Macro(TM)) everywhere, and it is entirely possible that bool is redefined as something else...

EDIT: Of course it is also possible that the error is in the last line before the one you pasted. Maybe you forgot the closing parenthese in the line before, then the error would show in that place, too.
« Last Edit: March 23, 2006, 05:54:29 pm by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: bool in static libs?
« Reply #2 on: March 23, 2006, 06:22:11 pm »
Here is a good example of what can happen to you due to CUMs:

Code
// 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?
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Rassilon

  • Guest
Re: bool in static libs?
« Reply #3 on: March 24, 2006, 09:35:28 pm »

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?

lol you sound like me when I rattle on microshaft...

I tend to try to stay away from mixing code like that anyways but I fully know what you mean...

The problem I had above I failed to name my file with the cpp extension...After doing so not only did bool become defined but I also was able to link my library successfully in the main project...

I see that you cannot mix C static libs with c++ ones!

Thanks!

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: bool in static libs?
« Reply #4 on: March 24, 2006, 09:41:41 pm »
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).
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: bool in static libs?
« Reply #5 on: March 24, 2006, 10:30:43 pm »
Well I guess it would be a normal part of windows programming to run you proper through the preprocessor to make sure it doesn't get mangled by windows macros.  That would probably save you all those days.

Rassilon

  • Guest
Re: bool in static libs?
« Reply #6 on: March 24, 2006, 10:53:00 pm »
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 did that actually...I believe I even tried extern on the function....And for some reason CB/link.exe would not see the library until I changed the file ext to cpp...

It could stem from my lack of experience using CB...When I first started I had a hell of a time getting CB to see the linked libraries due to the fact I was combining the Microsoft SDK with the toolkit...Copying all libs and includes over to the toolkit's directories...(Wanted to make my own easy install CD vs the current)...

It might not have been CB but the toolkit link.exe...I found it rather strange but atleast I got things working...

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: bool in static libs?
« Reply #7 on: March 24, 2006, 10:56:23 pm »
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.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: bool in static libs?
« Reply #8 on: March 24, 2006, 11:10:35 pm »
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. ;)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: bool in static libs?
« Reply #9 on: March 24, 2006, 11:17:02 pm »
No, but my answer to the statement that C libraries couldn't be used with C++ ones did. True, the question of whether they can or not has no bearing on the solution to Rassilon's problem...
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Rassilon

  • Guest
Re: bool in static libs?
« Reply #10 on: March 24, 2006, 11:25:04 pm »
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.

I misunderstood what you ment by extern...That method I didnt try so that would probably have fixed it....

I come from a long line of Visual Studio 4,5 and 6 Usage...Before that it was the ole BC 3.1...I know that Visual is rather lax when it comes to coding specifications...You could slap just about anything together and call it a static library...