Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including c++ standard headers from C not allowed, what should I use instead?

Tags:

c++

c

std

I'm trying to modernize an old code base that's written in a combination of C and C++.

My first task is to get it compiling with cl.exe v19.44.35214.

I'm getting a lot of this error in yvals_core.h:

#error: error STL1003: Unexpected compiler, expected C++ compiler.

After some digging, it looks like sometime in 2020-21 the standard library made the decision to prevent C files from using the C++ standard library.

My C isn't really that strong anymore, and I'm wondering what I should be using as replacements? The files I'm including that ultimately include yvals_core.h are:

  • <stdio.h>
  • <windows.h>
  • <cassert>
  • <cmath>
like image 439
Andy Avatar asked Oct 11 '25 12:10

Andy


2 Answers

Replace <cmath> with <math.h> in all files. <cmath> is only in C++.

Additionally, compile with /TC for C files & /TP for C++ files.

like image 114
0ro2 Avatar answered Oct 14 '25 02:10

0ro2


it looks like sometime in 2020-21 the standard library made the decision to prevent C files using the C++ standard library.

Well, this seems very reasonable.

As also pointed out by 0ro2, if that files are the only include problem, then you can just replace <cmath> with <math.h> and <cassert> with <assert.h>.

HOWEVER

  • Are you sure that the application doesn't use any C++ construct? Why is important to switch to a C compiler?

  • Please remember that C++ is not a superset of C. A C program doesn't necessarily compile/execute correctly when compiled with a C++ compiler and, of course, vice versa. See here for some examples. The "downgrading process" may not be as easy as it seems and may hide subtle bugs.

like image 21
ocirocir Avatar answered Oct 14 '25 01:10

ocirocir