visual c++ min max warning when including armadillo library - c++

I use the armadillo library in visual C++ to take care of some matrix and vector operations. Here's what one of my cpp files looks like:
#include "stdafx.h"
#include <cmath>
#include <armadillo>
#include "buildRotMatrix.h"
using namespace std;
using namespace arma;
//code
When I compile I get the following warning:
1>CL : warning : detected 'min' and/or 'max' macros and undefined them;
1>CL : warning : you may wish to define NOMINMAX before including any windows header
I don't actually use min and max functions in the code but i'd like to get rid of the warnings. adding #define NOMINMAX hasn't worked and I haven't found other solutions. It's not clear to me where the conflict comes from since my understanding from google searches is people get it from including windows.h.

Related

OpenCL 2.x(C++ bindings): undefined identifier

Edit: adding OpenCL.lib in library files section of visual studio solved the problem.
With cl2.hpp from this github repository pointed by khronos site, I get 60-70 similar errors when I try to compile a C++ dll project with OpenCL-C++-implementation functions to export.
'CL_DEVICE_QUEUE_ON_HOST_PROPERTIES': undeclared identifier
// I'm not using this, I'm newly converting a v1.2 C++ binding-based
// project to a v2.0 one
starting of the file is:
using namespace std;
#define __CL_ENABLE_EXCEPTIONS
#include <CL\cl2.hpp>
and the remaining lines are not underlined with red(visual studio). There are some compile time constants I need to add maybe, but I don't know which constants.
What is missing?
this code(there isn't any other code in project) also gives same error:
#include <CL\cl2.hpp>
I had the same errors and something that worked for me was to add the following prior to #include <cl2.hpp>
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
#include <cl2.hpp>

C++ cout gives undeclared identifier

So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?

Boost build error with websocketpp and MySQL on Windows

I am trying to build a C++ app that uses both websocketpp and MySQL. I have encountered 2 build problems using VS 2010 C++ Express.
1) A problem with the boost libraries. It produces many errors like this:
1>c:\program files (x86)\boost\boost_1_50\boost\thread\win32\thread_data.hpp(210): error C2146: syntax error : missing ')' before identifier 'rel_time'
Here's the relevant snippet from thread_data.hpp starting with line 210:
inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
{
interruptible_wait(detail::pin_to_zero(rel_time.total_milliseconds()));
}
2) A conflict with the word VERSION which is documented here and I believe is independent.
To make a clear and simple example of the boost build problems, I'm using the websocketpp example: echo_server.cpp to which I added these includes:
#include "stdafx.h"
Boost lib includes recommended by "Building a program with websocketpp" on the websocketpp site.
#include <boost/regex.hpp>
#include <boost/random.hpp>
#include <boost/system/api_config.hpp>
#include <boost/system/config.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/windows_error.hpp>
and the MySQL header includes. Adding these 2 boostincludes triggers the build errors. If I comment out these 2 includes, it builds without errors:
#include <my_global.h>
#include <mysql.h>
Any suggestions on how to deal with the boost problems?
I don't think this is the same build problem as this one, "Trying to build websocket++ with MinGW: last few linker errors — what could it be?"
Concerning the first error, check if there are any macros interfering with the code. Right-click and go to definition or #define the macro yourself at the beginning of the file and see where it gets redefined. In really hard cases, look at the preprocessor output via a compiler flag.
Concerning the rest, you don't provide any versions for Boost and MySQL. Then, there is my_global.h (or is that part of MySQL?) and stdafx.h, which are both under your control but you don't show them here. Also, try to reduce the issue to the smallest possible piece of code. In short, provide a reproducible example.

Is it possible to force visual studios to throw an error if an identifiers is used when its library isn't directly included in the source?

When compiling the following source:
int main()
{
exp(1.0);
return 0;
}
the copiler gives the following error: error C3861: 'exp': identifier not found because I didn't have the line: #include <iostream> above main()
However, visual studios won't display the error if a library is indirectly included. For example, the following code compiles without a problem even though the dependency of exp is in <cmath>.
#include <istream>
int main()
{
exp(1.0);
return 0;
}
This is because <iostream> includes <istream> which include <ostream> which includes <ios> which includes <xlocnum> which includes <cmath>.
Is there a way to make visual studios throw an error if I don't explicitly include a library yet try to use one of its identifiers?
You may want to have a look at include-what-you-use. It is a clang-based tool trying to detect missing and superfluous include directives.

How do I get a handle to split_winmain

I am trying to get a get the boost library program_options working on a simple windows console library.
I have linked in the library
C:\Program Files\boost\boost_1_40\lib\libboost_program_options-vc90-s-1_40.lib
Included the header files
#include <boost/program_options.hpp>
#include <boost/program_options/config.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/detail/cmdline.hpp>
#include <boost/program_options/detail/parsers.hpp >
Defined _WIN32 (But I don't think it is required.)
And I still keep getting the
Error 1 error C3861: 'split_winmain': identifier not found
It should be so simple but I can't get it to work. Can anyone tell me what I need to do here.
Joseph Shanahan
That function is declared in the boost::program_options namespace. If all you do is use its name alone, the compiler doesn't know what you're talking about. You have a few options:
Use the fully qualified name when you call it:
boost::program_options::split_winmain(...);
Tell the compiler which function you mean:
using boost::program_options::split_winmain;
split_winmain(...);
Bring the entire namespace into the current scope:
using namespace boost::program_options;
split_winmain(...);
Make a namespace alias:
namespace po = boost::program_options;
po::split_winmain(...);
I prefer the last one.
Do not define the _WIN32 macro; the compiler will do that for you when it's appropriate.