cmath.h and compile errors
I used to work with math.h without any problem. Now, I use an external library which itself has a file called math.h
, but which includes < cmath>
.
Adding this library to my project (or even just adding the include director开发者_开发知识库y, without touching the code) now generates tons of errors from < cmath>
:
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2039: 'acosf' : is not a member of '`global namespace''
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2873: 'acosf' : symbol cannot be used in a using-declaration
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2039: 'asinf' : is not a member of '`global namespace''
C:\Program Files\Microsoft Visual Studio 8\VC\include\cmath(18) : error C2873: 'asinf' : symbol cannot be used in a using-declaration
[etc, etc...]
I don't understand why this happens. I am using Visual Studio 2005 and looking on the internet, it seems that this problem is solved under VS 2008. However, I'd like to stay on VS 2005...
Including using namespace std;
everywhere, or changing the order of my includes doesn't seem to change anything. Defining _STD_BEGIN
solves the error, but produce as many in < xlocinfo>
.
How can this be solved?
Same Problem exists in VC 10. I think, that <cmath>
includes itself a math.h
but insted of the correct one, which is shipped with VC it uses the one which is created in the User-Project (with different content of course).
Solution: Do never use a File named math.h
in your Project... (Or correct the std somewhere).
I'm not sure I read your question correctly but it seems odd that a library would ship it's own math.h file.
Perhaps you are suppose to put the parent directory in your include path so that <my_lib/math.h>
can be included without conflicting with your compiler <math.h>
?
The problem is probably mixing C libraries with C++ conventions. For instance:
#include <math.h>
namespace TEST {
}
This compiles fine, whereas:
namespace TEST {
#include <math.h>
}
This generates a large number of spurious errors.
Just to confuse the issue:
#include <math.h>
namespace TEST {
#include <math.h>
}
This also compiles as it can only be included once (the first time).
Hence also:
#include <math.h>
namespace TEST {
#include "SomethingThatIncludesMath.h"
}
Will work, whereas:
namespace TEST {
#include "SomethingThatIncludesMath.h"
}
Won't.
You can also get similar problems by including C++ headers into a *.c file, rather than a *.cpp file.
I am sure that other similar mixing of C and C++ can lead to similar problems.
(1) According to Microsoft, the C2873 means;
'symbol' : symbol cannot be used in a using-declaration A using directive is missing a namespace keyword. This causes the compiler to misinterpret the code as a using declaration rather than a using directive.
(2) Also when I had C2873 with C2039 (I tried to merge CEF3 and Cinder), somehow I bypassed the both error by changing Properties->Configuration Properties->C/C++->Code Generation;
Enable Minimal Rebuild: Yes(/Gm), Enable C++ Exception: Yes(/EHsc), Enable Function-Level Linking: empty
精彩评论