VS2017 'M_PI': undeclared identifier [duplicate] - c++

This question already has answers here:
M_PI works with math.h but not with cmath in Visual Studio
(7 answers)
Closed 4 years ago.
I'm trying to compile a library in C++ as dll windows with visual studio 2017.
I have all C++/H compiled with clang 3.8 for android and it works.
I created a new project as dynamic library with LLVM as platform toolset. Now I'm trying to build but there is an error :
Error C2065 'M_PI': undeclared identifier
This error is present in many .cpp files. All linked .h contains :
#include <cmath>
I replaced by :
#define _USE_MATH_DEFINES // for C++
#include <cmath>
But it change nothing. What can I do ?

This is a known issue, try defining _USE_MATH_DEFINES as a definition in your project properties instead of the file itself.
This can be found in project properties -> C/C++ -> Preprocessor -> Preprocessor definitions.

Related

visual studio c++ cannot open any source file [duplicate]

This question already has answers here:
Cannot open include file: 'ctype.h': No such file or directory
(4 answers)
Visual Studio 2022: Cannot open include file: 'ctype.h'
(1 answer)
Closed 7 months ago.
I've been using visual studio for my unreal projects for some time now.
But when i try to code something in vs i get the error "cannot open source file".
I have a very basic c++ code;
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
when i debug this code i get the error that almost every header file(435 header files) like float.h, string.h, time.h etc. cannot open.
When i try compiling the code using gcc i get another error but when i use g++ i get no error.
So i'm assuming i need to change debug options of visual studio. I've tried buy i couldn't figure that out.
How can i make visual studio run the code properly?

Visual C++ : XGBoost does not work when called from a DLL

I have a requirement to use XGBoost within a Visual C++ project DLL.
I have been able to download and build the XGBoost source using VC++ and CMake.
When I include the XGBoost code in a test console application, it works fine.
However, when I try to replicate what I've done with the console application in a DLL project, it won't compile.
I am struggling to even use a basic XGBoost type within the project.
I suspect the problem is my ignorance with DLL projects and would appreciate your help.
Here's what's happening in my DLL project:
When I use the following include as the very first line in a cpp class file, it compiles:
#include <xgboost\c_api.h>
With it compiling, if I try to use a simple type defined in this include file, the build fails with the following message:
...\dll_test\xgb_driver.cpp(20): error C2065: 'BoosterHandle': undeclared identifier
This is the line that causes the error:
BoosterHandle my_handle;
"BoosterHandle" is in fact defined in <xgboost\c_api.h>
When I put the include below any other include, I get the following error messages:
1>c:\tools\src\xgboost\include\xgboost\c_api.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\tools\src\xgboost\include\xgboost\c_api.h(29): error C2146: syntax error: missing ';' before identifier 'bst_ulong'
Below is a little more information on what I did to get XGBoost working with a console app and how I created the DLL project.
=-=-=-=-=-=
Here's what I did to use XGBoost with a console. Everything about it seems to work. I've tested the model predictions, and they are
consistent with what I'm seeing in R.
Using the documentation found here:
https://xgboost.readthedocs.io/en/latest/build.html
I downloaded the XGBoost source and built it using CMake and Visual Studio 2015.
Under Project > Linker > Input > Additional Dependencies,
I added the xgboost.lib file
Under Project > Linker > General > Additional Library Directories
I added a reference to ...\xgboost\build\Release
Under Project > VC++ Directories > Include Directories
I added the path to ...\xgboost\rabit\include and ...\xgboost\include
I put the xgboost.dll in the directory where the .exe is generated.
From here, it was smooth sailing.
=-=-=-=-=-=
Here's what I've done to create a Visual C++ DLL Project:
After choosing File > New > Project, I select an ATL DLL Project (this is part of the project requirement).
For the XGBoost include to the project, I repeated steps 1-5 above, but placed the xgboost.dll file where the
project DLL would be generated.
Here is the source for the header file for the simple class I have created:
#pragma once
class XGB_Driver
{
public:
XGB_Driver();
~XGB_Driver();
float callXGB(float sample_input);
};
Here is the source for the simple cpp file:
#include <xgboost/c_api.h>
#include "stdafx.h"
#include "XGB_Driver.h"
XGB_Driver::XGB_Driver()
{
}
XGB_Driver::~XGB_Driver()
{
}
float XGB_Driver::callXGB(float simple_input) {
BoosterHandle my_handle;
return(0);
}
Why this doesn't work for the ATL DLL project, but does for the console app really has me banging my head against the keyboard.
Your help would be very much appreciated.
Best,
Dave
With the help of CristiFati, I believe this question has been answered.
For whatever reason, it seems that using XGBoost with C++ in a DLL project requires additional includes above and beyond what is required for using it in a console application.
I am currently building the DLL project with the addition of:
#include <cstdint>

HYPRE blas build error with VS 2015

I installed VS 2015 Professional. I installed the latest HYPRE, from the Lawrence Livermore website. I then configured it using CMake and proceeded to build, and I started getting BLAS (dnrm2.c) build errors:
2> dnrm2.c
2> 1>
C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\math.h(454): error C2059: syntax error: '('
The line of code triggering the error in dnrm2.c is:
#include "math.h"
which points to the file:
c:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\math.h
I looked up this error and found some suggestions such as this to change the include to:
#include <cmath>
and to edit the HYPRE project settings in: Configuration > C/C++ > Advanced > Compile As to Compile As C++ (/TP)
which I did, but I still see same error, since apparently the same header path to math.h is included from cmath as well:
#else /* _STD_USING */
#include <math.h>
#endif /* _STD_USING */
I've even tried re-installing VS 2015 without any luck (same errors). Appreciate any ideas on what's going on here, and how to resolve this. I guess I could try a minimalist example in VS 2015 that includes the math.h and report back, if that helps.
EDIT
My minimalist example:
#include "math.h"
int main() {
double d1 = sqrt(4.0);
float d2 = abs(4.0);
return 0;
}
appears to be building OK. I tried to set the project the same way to Compile as C (or C++, didn't matter). This doesn't really help me though.
OK, the problem here is with HYPRE source it looks like. They have this in a file f2c.h included before including the math.h:
//#undef abs
//#define abs(x) ((x) >= 0 ? (x) : -(x))
//#endif
When I commented it out (since this is already defined in the standard), then it gets past that build error. Of course I run into other build errors. I'm trying to tackle those separately.
EDIT: It's not as simple as that because they (HYPRE) actually rely on their own definition of abs. So I had undo the above and change the order of some includes so that the undef actually made sense. Either way, this is a HYPRE source code problem.
If you succeed in compiling the HYPRE on VS2015, Could you send your VS2015 program to me!
My major is Geophysics modeling and inversion.
MY email is schoolhui#hotmail.com
Thank you very much!
I've just commented
_Check_return_ int __cdecl abs(_In_ int _X);
in
c:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\math.h
and then HYPRE was successfully compiled!
Then, I've uncommented "abs".

NOMINMAX with Visual Studio 2012 MFC project [duplicate]

This question already has answers here:
Possible problems with NOMINMAX on Visual C++
(4 answers)
Closed 7 years ago.
I want to use #define NOMINMAX in my Visual Studio C++ project using MFC, so that I can use std::min and std::max. However, when I put this line in my stdafx.h, I get following compile error:
c:\program files (x86)\windows kits\8.0\include\um\GdiplusTypes.h(475): error C3861: 'min': identifier not found
I am not using GDI+ intentionally, this is something MFC must be doing. Can the issue be fixed somehow, either by removing the GDI+, or by adjusting it to compile?
I don't work on Windows so I'm not used to dealing with this, and I'm not testing this, but I believe that answer is suggesting you do this:
#define NOMINMAX
#include <algorithm>
namespace Gdiplus
{
using std::min;
using std::max;
};
//... your other includes.
This will get the "proper" versions of min and max, and make them available without the std:: prefix (which seems to be how it is used in the GdiplusTypes.h header).
The winapi min and max are macros, so you can just #undef them after including the MFC or winapi headers:
#undef min
#undef max

My Eclipse program dosn't recognize `NULL` [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
error: ‘NULL’ was not declared in this scope
i'm having this code, which has been written in visual studio but i`m working in eclipse and i'm trying to make it compilable for eclipse and i throws me this error
..\heap.cpp:104:10: error: 'NULL' was not declared in this scope
code:
#include"heap.h"
using namespace std;
template<class T>
Heap<T>::Heap() // constructor
{
root = NULL;
size = 0;
}
Eclipse isn't a compiler, just an IDE. I'm guessing you're using it with another compiler than Visual Studio and the system headers are somewhat different, leading to your VC++ working includes to not include the declaration of NULL on <the other compiler>. As Martinho Fernandes said, you need to include <stdlib.h> or <cstdlib>, or some header that includes those ones. As the other question says, the C++ 11 way would be <stddef.h> or <cstddef>.