Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL headers conflict with STL <algorithm> in C++

Tags:

c++

mysql

// File test.cpp
#include <my_global.h>
#include <algorithm>
int main()
{
    return 0;
}

Compiled with: g++ -c -I /usr/local/mysql/include/mysql/ test.cpp, where /usr/local/mysql is the mysql install directory.Then the compiler report the following errors:

In file included from /usr/include/c++/4.4/algorithm:61, from test.cpp:3: /usr/include/c++/4.4/bits/stl_algobase.h:232:56: error: macro "min" passed 3 arguments, but takes just 2 /usr/include/c++/4.4/bits/stl_algobase.h:253:56: error: macro "max" passed 3 arguments, but takes just 2 In file included from /usr/include/c++/4.4/bits/stl_algo.h:61, from /usr/include/c++/4.4/algorithm:62, from test.cpp:3: /usr/include/c++/4.4/bits/algorithmfwd.h:353:41: error: macro "max" passed 3 arguments, but takes just 2 /usr/include/c++/4.4/bits/algorithmfwd.h:364:41: error: macro "min" passed 3 arguments, but takes just 2 In file included from /usr/include/c++/4.4/algorithm:61, from test.cpp:3: /usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected unqualified-id before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected initializer before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected unqualified-id before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected initializer before ‘const’ /usr/include/c++/4.4/bits/stl_algobase.h:232: error: ‘std::min’ declared as an ‘inline’ variable /usr/include/c++/4.4/bits/stl_algobase.h:232: error: template declaration of ‘const _Tp& std::min’ /usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected primary-expression before ‘if’ /usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected ‘}’ before ‘if’ /usr/include/c++/4.4/bits/stl_algobase.h:237: error: expected unqualified-id before ‘return’ /usr/include/c++/4.4/bits/stl_algobase.h:253: error: ‘max’ declared as an ‘inline’ variable /usr/include/c++/4.4/bits/stl_algobase.h:253: error: template declaration of ‘const _Tp& max’ /usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected primary-expression before ‘if’ /usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected ‘}’ before ‘if’ /usr/include/c++/4.4/bits/stl_algobase.h:258: error: expected unqualified-id before ‘return’ /usr/include/c++/4.4/bits/stl_algobase.h:259: error: expected declaration before ‘}’ token

I think that there's some name conflict between my_global.h and algorithm, so I wrap my_global.h in a namespace:

// File test.cpp
namespace MYSQL_NAMESPACE {
    #include <my_global.h>
}
#include <algorithm>
int main()
{
    return 0;
}

But it doesn't help, the compiler still report the same errors. Then I change the include order as following:

// File test.cpp
#include <algorithm>
#include <my_global.h>
int main()
{
    return 0;
}

Every thing goes well now.

Does Anybody Really Know What The Problem It Is?

TKS!

like image 450
zJay Avatar asked Dec 04 '25 08:12

zJay


2 Answers

It seems that the mysql header defines a macro min.

#if !defined(max)
#define max(a, b)       ((a) > (b) ? (a) : (b))
#define min(a, b)       ((a) < (b) ? (a) : (b))
#endif

This has been reported to MySQL as bug 28184. The bug is marked as closed, so try updating to the newest version. According to the bug page it should be fixed in version 5.1.23, version 6.0.4 and newer versions.

like image 158
Mark Byers Avatar answered Dec 05 '25 21:12

Mark Byers


Apparently the namespace trick does not work because min/max are macros and the preprocessor does not look at namespace scope.

This might fix the problem:

#include <my_global.h>
#undef min
#undef max
#include <algorithm>

The whole thing looks horrible though :)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!