How to determine if libary C or C++

closed account (DEhqDjzh)
Hi, I am tying write a program in visual studio 2017. But sometimes c and c++ libarys get mixed and creating a diaster. is there an option in visual studio for avoid that or must i open a C reference and check if there is a library like that or not ?
C++ is designed to interoperate with C. What do you mean by, "get mixed and creating a diaster"?
closed account (DEhqDjzh)
@kbw when coding c, using iostream cause disaster
I haven't compiled many libraries with Visual Studio (usually use GCC).

If a library was written for C, you should include its header using extern "C".
1
2
3
4
#include <iostream> // for example
extern "C" {
#include "myclibrary.h"
}
@kbw when coding c, using iostream cause disaster
Can you give an example?

EDIT
You can't add C++ code to a C program. If that's what you're doing, it just won't work.

You can use a C++ library within a C program if it has a C interface.

You can use C code/libraries within C++ programs.
Last edited on
closed account (DEhqDjzh)
@kbw
1
2
3
4
5
6
7
8
 #include <stdio.h>
#include <iostream>
int main()
{
   
   printf("Hello, World!");
   return 0;
}

I got 2658 error in cmath+algorithm files if I use iostream
Hi, are you saying that simple example doesn't compile for you? I just tested out building it in VS 2017 and it works fine... if so, sounds like something more fundamental is wrong with your configuration.
Last edited on
That is not a valid C program. However, it is a valid C++ program.

As I recall, Visual Studio uses the file extension to decide how to compile something. If your file has C++ extension, it will use the C++ compiler and link with the C++ runtime libraries by default.

What is the name of your source file?

What is the exact text of the error message?
C++ originated as a subset of C, and it is still true that most C code will work in a C++ program.

The opposite is not true. There are many features that C++ has, that a C compiler will not understand. Trying to use a C++ standard library header is almost certain to cause compilation to fail.

As far as the standard library is concerned, it is normal for header files from the C standard library to end in .h. It is normal for header files from the C++ standard library to not have that extension. So, for example:

1
2
3
#include <stdio.h>  // This is from the C standard library
#include <iostream>  // This is from the C++ standard library
#include <cstdio>  // This is from the C++ library, and implements in C++ the things that are implemented in stdio.h in C 


So that can give you a big hint.

However, third-party libraries tend not to use that convention. Sometimes .hpp is used for C++ headers, so that can give you a big clue, but often .h is used for C++ as well as C, so you can't use that reliably.

The only sure way to know, is to put in the work to:

1) Learn the language you're programming in properly.
2) Read the documentation. You're on the internet, so this is all just a search away.
Last edited on
closed account (DEhqDjzh)
E0147 declaration is incompatible with "long double __cdecl powl(long double _X, long double _Y)" (declared at line 917 of "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt\corecrt_math.h") cp c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\cmath 650
and
E0020 identifier "using" is undefined cp c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\cmath 651
with 2394 similar errors like those two
identifier "using" is undefined

That should give you a pretty big hint, especially if you read my post in this thread.
Last edited on
As the others have now confirmed, you're building in C instead of C++. This will not work. You probably messed up your settings somehow.

With your project opened, go to Project --> Properties --> Configuration Properties --> C/C++ --> Advanced --> Compile As
Choose Compile as C++ code (/TP). Default should work, too, but as kbw mentioned, I think it might look at the individual file extensions. You aren't answering a lot of questions, so it's hard to help further.
closed account (DEhqDjzh)
Thank you all so much
Topic archived. No new replies allowed.