VS2012 mixing C and Cpp, library clash - c++

I am trying to combine some C and C++ code in a project, and before I had no issues with this, but I'm now trying out VS2012 for the first time and I get a lot of errors from a TR1 header that I didn't include explicitely.
I'm thinking it might be a mixup because it's trying to compile my c code with this cpp header. It looks like a C compiler error trying to understand C++ template code. Where can I figure this out from the VS2012 interface? Do I need to do something to my c and cpp files to make the compiler treat them properly?
Error 1 error C2143: syntax error : missing '{' before '<' C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xtr1common 15 1 example~
Error 2 error C2059: syntax error : '<' C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xtr1common 15 1 example~
Error 3 error C2143: syntax error : missing '{' before '<' C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xtr1common 20 1 example~
Error 4 error C2059: syntax error : '<' C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xtr1common 20 1 example~
Error 5 error C2016: C requires that a struct or union has at least one member C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xtr1common 28 1 example~

Never mind. It has been a while since I've used VS and forgot that it doesn't necessarily treat cpp files as c++. I had to set the "compile as" option to c++.

Related

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.

Syntax errors when trying to use CString in Visual C++

My project is not happy regarding include cstring. It generates errors like:
Error 10 error C2061: syntax error : identifier 'using' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\cstring 18 1 fgdll
Error 95 error C2061: syntax error : identifier 'tanhf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\cmath 26 1 fgdll
Error 93 error C2061: syntax error : identifier 'tanf' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\cmath 26 1 fgdll
Error 51 error C2061: syntax error : identifier 'strxfrm' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\cstring 26 1 fgdll
Error 49 error C2061: syntax error : identifier 'strtok' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\cstring 26 1 fgdll
Error 47 error C2061: syntax error : identifier 'strstr' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\cstring 25 1 fgdll
...
How can I solve this problem?
Ok, try this:
Right click on each piece of code and go to:
Properties > C/C++ > Advanced > Compile As and check Compile as C++ Code(/TP)

Moving C++ project, errors in atlplus.h

I am trying to move an old C++ project to VS2010.
The last version of VS to compile it was VS2005, as far as I know. I already fixed a bunch of errors but now I am stuck,
since I am getting errors in atlplus.h, which is a VC10 header file.
Error 1 error C2065: 'CStringW' : undeclared identifier C:\Program
Files (x86)\Microsoft Visual Studio
10.0\VC\atlmfc\include\atlplus.h 692 1
Error 2 error C2146: syntax error : missing ';' before
identifier 'strW' C:\Program Files
(x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\atlplus.h 692 1
Error 3 error C3861: 'strW': identifier not found
C:\Program Files
(x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\atlplus.h 692 1
Error 4 error C2065: 'strW' : undeclared identifier C:\Program Files
(x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\atlplus.h 693 1
Does anyone have any idea? I've been stuck here since last week.
You need to include atlstr.h before any headers that use CString.
CString has been moved to the atlstr.h header file.
I met the similar problem. please try to include atlbase.h instead of atlstr.h.
You can see a comment line in file atlplus.h:
//Avoid using <atlstr.h> except in the registry templates (for circular dependencies).

Why do I get errors when I #include curl.h?

I'm trying to get libcurl to work with my C++ project.
I've added libcurl to my project.
I've also added the header files, preprocessor and linker configuration.
When adding #include to my project I get a lot of winnt.h errors:
c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winnt.h(1084): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winnt.h(1084): error C2059: syntax error : '('
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winnt.h(1084): error C2090: function returns array
...
What is the reason to this problem?
Thanks.

Error building legacy code with VS 2005

Trying to build a legacy code in VS2005 and get errors in VC header files.
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2096) : error C2065: '_Sb' : undeclared identifier
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2176) : see reference to class template instantiation 'std::istreambuf_iterator<_Elem,_Traits>' being compiled
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2096) : error C3861: '_Strbuf': identifier not found
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2096) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2096) : error C2061: syntax error : identifier '_Sb'
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2097) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2098) : error C2143: syntax error : missing ';' before '{'
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2098) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
d:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(2176) : fatal error C1075: end of file found before the left brace '{' at 'd:\Compilers\Microsoft Visual Studio 8\VC\include\xutility(15)' was matched
I find the legacy code uses a vector and if I comment out those variables then the code compiles without any errors.
I get these errors even if I just include vector header without defining any variable.
All the files in the project are cpp files.
Do I need to add some preprocessor directives or compiler settings to get this working?
You may find that running the pre-process phase on one of the files might show if any macros are being expanded that conflict with vector or _Strbuf. See my post on how to do this:
Compiling a project (VS 2008) with the /p argument (preprocess to a file) doesn't compile
You probably need to remove some conflicting predefined macros. Leading underscores followed by uppercase letters, as in _Sb are reserved for the implementation. If you have that defined as something else, you'll get such errors. It's also possible the errors are because you've defined _Strbuf ot _Got.