Adding AdditionalIncludeDirectories makes project unable to import from standard library - c++

I have a project in Visual Studio that is set up with the following architecture:
src
main.cpp
math.cpp
include
math.h
math.h contains function prototypes for the functions found in math.cpp. When using #include "math.h" after adding include\ to the project's C/C++ Additional Include Directories, I find the compiler is not able to find the standard library. I get several errors like this:
...
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(261,18): error C2039: 'sinhf': is not a member of '`global namespace''
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(261,23): error C3861: 'sinhf': identifier not found
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(265,18): error C2039: 'sqrtf': is not a member of '`global namespace''
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(265,23): fatal error C1003: error count exceeds 100; stopping compilation
Of course, I can successfully include include\ by using #include "..\include\math.h" but I know there must be a way to tell Visual Studio to look in \include for files to include. Any ideas on what I am doing incorrectly?

Related

Visual Studio All c++ functions are not members of global namespace

I recently installed Visual studio as I'm working to pick up c++ more. However in c++, even running something as simple as a hello world code yields me a huge number of errors such as
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\cstdlib(24,18): error C2039: 'fabs': is not a member of 'global namespace'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\cstdlib(24,22): error C3861: 'fabs': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\cstdlib(28,18): error C2039: 'fabsf': is not a member of 'global namespace'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\cstdlib(28,23): error C3861: 'fabsf': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\cstdlib(32,18): error C2039: 'fabsl': is not a member of 'global namespace'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\cstdlib(32,23): error C3861: 'fabsl': identifier not found
The error seems to be either an issue with my VS setup or my C++ Distribution. Is this a common issue? Has anyone else seen this before?
Here is the code
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}

ctime Error when including boost::filesystem

I am working on small project. The essential part is, that a video should be captured, saved and moved to another folder.
For the video part I am using FFmpeg this works without any problems. To safe information I am using tinyXML2. For copying and moving of the video files I like to use boost.
At the moment I am working on windows, and I am using CMake.
The problem is, that in the moment I include the "boost/filesystem" the compiler starts failing. After the use of google and analyzing the error messages I think that there is a problem with headers <ctime> or <time>. The file that is using <ctime> is xmltest that is passed by tinyxml2.
But to be honest I am very much clueless how and why this happens.
I already tried to switch the order of the includes as mentioned here.
I tried to exclude the file that uses ctime, but this is not possible.
I tried to work around the problem, so I don't have to use Boost, but this is hard due to the platform independency.
The log file:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(19): error C2039: 'clock_t': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(19): error C2873: 'clock_t': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(23): error C2039: 'asctime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(23): error C2873: 'asctime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(24): error C2039: 'clock': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(24): error C2873: 'clock': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(25): error C2039: 'ctime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(25): error C2873: 'ctime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(26): error C2039: 'difftime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(26): error C2873: 'difftime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(27): error C2039: 'gmtime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(27): error C2873: 'gmtime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(28): error C2039: 'localtime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(28): error C2873: 'localtime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(29): error C2039: 'mktime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(29): error C2873: 'mktime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(30): error C2039: 'strftime': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(30): error C2873: 'strftime': symbol cannot be used in a using-declaration
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(31): error C2039: 'time': is not a member of '`global namespace''
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\ctime(31): error C2873: 'time': symbol cannot be used in a using-declaration
The way I include Boost & tinyxml:
find_package(TinyXML2)
find_package(Boost COMPONENTS filesystem system REQUIRED)
...
add_executable(programm ${project_sources}
${project_headers}
${TINYXML_INC})
target_link_libraries(programm PUBLIC
Boost::filesystem
Boost::system
${TinyXML2_LIBRARIES})
target_include_directories(programm PRIVATE
${Boost_INCLUDE_DIRS}
${Boost_LIBRARY_DIRS}
${TinyXML2_INCLUDE_DIR})
target_link_directories(programm PUBLIC
${Boost_INCLUDE_DIRS}
${Boost_LIBRARY_DIRS}
${TinyXML2_INCLUDE_DIR})
The include of Boost in Util.h:
#include <string>
#include <boost/filesystem.hpp>
namespace util {
bool existens_file(std::string* path);
int existens_directory(std::string* path);
bool create_directory(std::string* path);
}
Part in xmltest where <ctime> is used:
#include "tinyxml2.h"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
...
XMLDocument* doc = new XMLDocument();
clock_t startTime = clock();
doc->LoadFile( argv[1] );
clock_t loadTime = clock();
int errorID = doc->ErrorID();
delete doc; doc = 0;
clock_t deleteTime = clock();
EDIT
After some Testing I tried to use <ctime> in diffrent files without the include of boost. In this case I also receive the same problems as mentioned above.
So I fixed the issue, in the ffmpeg-library is file that is called time.h or Time.h.
The misstake was that accidentally linked the directory that contained this file.
I don't know why but with the include of boost::filesystem this was triggered and the time-file of ffmpeg was used.
I resolved this problem, through removing the linked directory of ffmpeg.

Libs ,DLLs and .h files

Im a C# guy thats why I'm lost here, this project claims to have compiled that library for Windows https://soildgeo.codeplex.com/releases/view/108738
the folder comes with Include,Lib64 and Bin64 folders. If I'm not mistaken, I only need to copy .lib files to the VC/lib folder and the folders in include to VC/include folder, so that I can start coding against these APIs, they appear in the #include<> intellisense but then they don't compile. my questions are, are my assumptions on this whole process is right ? if not what are the right steps ? and lastly, do I need to use the Dlls found in Bin64 folder at any point ?
Im using VS2013
Thanks in advance
Edit
1>------ Build started: Project: PhysBAMTest, Configuration: Debug x64 ------
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2146: syntax error : missing ';' before identifier 'attribute'
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2530: 'PhysBAM::LOG::anonymous-namespace'::cout' : references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2065: 'unused' : undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163): error C2143: syntax error : missing ';' before '='
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2146: syntax error : missing ';' before identifier '__attribute__'
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2530: 'PhysBAM::LOG::anonymous-namespace'::cerr' : references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2065: 'unused' : undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2374: 'PhysBAM::LOG::anonymous-namespace'::__attribute__' : redefinition; multiple initialization
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(163) : see declaration of 'PhysBAM::LOG::anonymous-namespace'::attribute'
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\PhysBAM_Tools\Log\LOG.h(164): error C2143: syntax error : missing ';' before '='
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Edit
after adding the WIN32 macro based on an answer below , I got a linker error this time
Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl PhysBAM::LOG::cout_Helper(void)" (?cout_Helper#LOG#PhysBAM##YAAEAV?$basic_ostream#DU?$char_traits#D#std###std##XZ) referenced in function "void __cdecl PhysBAM::LOG::anonymous namespace'::dynamic initializer for 'cout''(void)" (??__Ecout#?A0x5bbeb782#LOG#PhysBAM##YAXXZ) C:\Users...\Documents\Visual Studio 2013\Projects\PhysBAMTest\PhysBAMTest\main.obj PhysBAMTest
All those attribute, unused, __attribute__ are what some call GNU-ishms, that is, features specific to the GCC compatible compilers (GCC itself, Clang, ICC and others). But MS VC does not aim for GCC compatibility, so they just don't work.
The people that did the port to Windows managed to remove these construct by using a few precompiler tricks, I think in the file Utilities/PHYSBAM_OVERRIDE.h. But for those to work you have to define the macro WIN32. Do that in the project "C/C++ Preprocessor Settings" page, not in the code, so it will be defined for all the source files at once.
Alternatively, and this IMO would be the correct solution, patch the sources, and replace every occurrence of #ifdef WIN32 or #if defined(WIN32) with #ifdef _WIN32. The macro _WIN32 is always predefined in Win32 and Win64, but the WIN32 is not.
Well, technically you'd have to differentiate each occurence if the issue is about being a Windows system or about being a MSVC compiler (there is a GCC compiler for Windows). And then use _WIN32 for the system dependencies and _MSC_VER for the compiler ones.
And then, if you feel like it, send a patch to the Codeplex project.
UPDATE
About the linker error, that should be easy to fix: just add the necessary "*.lib" file or files to the project. You can add them to the Linker Property Pageof the project.
Additionally what you did with .lib and .h, you need to keep .dll in either in, System32 dir, or in same dir where you have exe.
Answers to this question may help you understand better.

Errors building for Windows XP with Visual Studio 2013 Express

EDIT: Solved! See bottom.
I am trying to make my C++/OpenGL game compatible with windows XP.
Currently I am using Visual Studio 2013 express. I followed the instructions here and changed the Platform Toolset to v120_xp, but now I am getting errors in winbase.h and some other files.
My windows specific includes look like this:
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <direct.h>
#include <sys/types.h>
#include <dirent.h>
Here are some of the errors I am getting:
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\winbase.h(6133): error C2226: syntax error : unexpected type 'LPSTR'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\winbase.h(6141): error C2226: syntax error : unexpected type 'LPWSTR'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\stralign.h(113): error C2065: 'wcscpy_instead_use_StringCchCopyW_or_StringCbCopyW' : undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\cstdio(46): error C2039: '_gets_instead_use_StringCchGetsA_or_StringCbGetsA' : is not a member of '`global namespace''
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\cstring(20): error C2039: 'strcat_instead_use_StringCchCatA_or_StringCbCatA' : is not a member of '`global namespace''
There are many other similar errors.
I would provide more code but this is quite a large project. If there is anything specific you need let me know. Any help would be appreciated!
Solution:
I figured it out when following Jerry's advice.
In one of my files I was including tchar.h and strsafe.h before Windows.h
Including Windows.h above them solved the problem.

file writing in vs2010 (c++, win32)

I asked this question earlier and you told me to mention the errors,
so I will mention them now(I couldn't figure out how to continue the last thread I started,all I saw was an "add a comment" button and an "answer your question" button, so I had to correct the questio and ask it again, sorry about that):
My question is the following:
I'm using visual studio 2010 and I'm writing a win32 application(not a console application).
I need to know how to write to a file from this application.
I included these headers: windows.h, stdlib.h, string.h and tchar.h
I wrote a very simple hello world application and it ran just fine.
But when I tried to include iostream and fstream
in my project the compiler gave me the following errors.
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(21): error C2039: 'abort' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(21): error C2873: 'abort' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2039: 'exit' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2873: 'exit' : symbol cannot be used in a using-declaration
IntelliSense: the global scope has no "abort" c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib 21 13
IntelliSense: the global scope has no "exit" c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib 24 13
And when I include fstream.h,I get:
error C1083: Cannot open include file: 'fstream.h': No such file or directory c:\users\user\documents\visual studio 2010\projects\helloworld\helloworld\main.cpp 5 1 helloworld
IntelliSense: cannot open source file "fstream.h" c:\users\user\documents\visual studio 2010\projects\helloworld\helloworld\main.cpp 5 1 helloworld
Same thing for iostream.h
Why are these errors occurring?
In C++, you should use <cstdlib> instead of <stdlib.h>, <cstring> instead of <string.h> (assuming you mean the C style strings. If you want C++ std::string, use <string> [without .h].
And you should use <fstream>, not <fstream.h>.
Please check that your :
#include "stdafx.h"
is the first include of you .cpp file.
Write other includes after this one :
#include "stdafx.h"
#include <iostream>
// ... and so on
int main(...) {
}
... this is a quite frequent mistake with Microsoft compilers (see C++ cout gives undeclared identifier).
Probably you wrote #include "iostream" instead of #include <iostream>
May be this helps .. Try to Check your errors reasons from
http://msdn.microsoft.com/en-us/library/et4zwx34%28v=vs.80%29.aspx