2

I'm new to mixing C & C++ code. Understood the need of extern & __cplusplus directives after reading some SO links & online reading. Not sure why am I getting the error. Did i miss something ?

C++ header: cppexh.h

#include <iostream>

#ifdef __cplusplus
extern "C" {
#endif

void Callme(int a);

#ifdef __cplusplus
}
#endif

C++ Source

#include "cppexh.h"

void Callme(int a)
{
    std::cout << "Val is " << a << std::endl;
}

C Code calling C++ function

#include <stdio.h>
#include "cppexh.h"

int main()
{
    CallMe(2);
    retun 1;
}

But I'm getting compilation errors:

1>------ Rebuild All started: Project: CProject, Configuration: Debug Win32 ------
1>  Ccode.c
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtgmath.h(214): warning C4602: #pragma pop_macro : 'new' no previous #pragma push_macro for this identifier
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtgmath.h(215): warning C4193: #pragma warning(pop) : no matching '#pragma warning(push)'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtgmath.h(216): warning C4161: #pragma pack(pop...) : more pops than pushes
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(23): error C2061: syntax error : identifier 'abs'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(23): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(23): error C2061: syntax error : identifier 'acos'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(23): error C2061: syntax error : identifier 'asin'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(24): error C2061: syntax error : identifier 'atan'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(24): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(24): error C2061: syntax error : identifier 'atan2'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(24): error C2061: syntax error : identifier 'ceil'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(25): error C2061: syntax error : identifier 'cos'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(25): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(25): error C2061: syntax error : identifier 'cosh'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(25): error C2061: syntax error : identifier 'exp'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(26): error C2061: syntax error : identifier 'fabs'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(26): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(26): error C2061: syntax error : identifier 'floor'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(26): error C2061: syntax error : identifier 'fmod'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(27): error C2061: syntax error : identifier 'frexp'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(27): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(27): error C2061: syntax error : identifier 'ldexp'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(27): error C2061: syntax error : identifier 'log'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(28): error C2061: syntax error : identifier 'log10'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(28): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(28): error C2061: syntax error : identifier 'modf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(28): error C2061: syntax error : identifier 'pow'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(29): error C2061: syntax error : identifier 'sin'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(29): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(29): error C2061: syntax error : identifier 'sinh'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(29): error C2061: syntax error : identifier 'sqrt'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(30): error C2061: syntax error : identifier 'tan'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(30): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(30): error C2061: syntax error : identifier 'tanh'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(32): error C2061: syntax error : identifier 'acosf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(32): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(32): error C2061: syntax error : identifier 'asinf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(33): error C2061: syntax error : identifier 'atanf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(33): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(33): error C2061: syntax error : identifier 'atan2f'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(33): error C2061: syntax error : identifier 'ceilf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(34): error C2061: syntax error : identifier 'cosf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(34): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(34): error C2061: syntax error : identifier 'coshf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(34): error C2061: syntax error : identifier 'expf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(35): error C2061: syntax error : identifier 'fabsf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(35): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(35): error C2061: syntax error : identifier 'floorf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(35): error C2061: syntax error : identifier 'fmodf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(36): error C2061: syntax error : identifier 'frexpf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(36): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(36): error C2061: syntax error : identifier 'ldexpf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(36): error C2061: syntax error : identifier 'logf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(37): error C2061: syntax error : identifier 'log10f'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(37): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(37): error C2061: syntax error : identifier 'modff'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(37): error C2061: syntax error : identifier 'powf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(38): error C2061: syntax error : identifier 'sinf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(38): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(38): error C2061: syntax error : identifier 'sinhf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(38): error C2061: syntax error : identifier 'sqrtf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(39): error C2061: syntax error : identifier 'tanf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(39): error C2061: syntax error : identifier 'tanhf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(41): error C2061: syntax error : identifier 'acosl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(41): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(41): error C2061: syntax error : identifier 'asinl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(42): error C2061: syntax error : identifier 'atanl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(42): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(42): error C2061: syntax error : identifier 'atan2l'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(42): error C2061: syntax error : identifier 'ceill'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(43): error C2061: syntax error : identifier 'cosl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(43): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(43): error C2061: syntax error : identifier 'coshl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(43): error C2061: syntax error : identifier 'expl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(44): error C2061: syntax error : identifier 'fabsl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(44): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(44): error C2061: syntax error : identifier 'floorl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(44): error C2061: syntax error : identifier 'fmodl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(45): error C2061: syntax error : identifier 'frexpl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(45): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(45): error C2061: syntax error : identifier 'ldexpl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(45): error C2061: syntax error : identifier 'logl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(46): error C2061: syntax error : identifier 'log10l'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(46): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(46): error C2061: syntax error : identifier 'modfl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(46): error C2061: syntax error : identifier 'powl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(47): error C2061: syntax error : identifier 'sinl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(47): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(47): error C2061: syntax error : identifier 'sinhl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(47): error C2061: syntax error : identifier 'sqrtl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(48): error C2061: syntax error : identifier 'tanl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(48): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(48): error C2061: syntax error : identifier 'tanhl'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(50): error C2054: expected '(' to follow 'using'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(50): error C2061: syntax error : identifier 'using'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(52): error C2061: syntax error : identifier 'acosh'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(52): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(52): error C2061: syntax error : identifier 'asinh'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(52): error C2061: syntax error : identifier 'atanh'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(53): error C2061: syntax error : identifier 'cbrt'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(53): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(53): error C2061: syntax error : identifier 'erf'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(53): error C2061: syntax error : identifier 'erfc'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\cmath(53): fatal error C1003: error count exceeds 100; stopping compilation
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

VERSION 2 OF THE CODE

EDITED THE CODE WHICH SOLVED THE COMPILATION ISSUES BUT THE LINK ERROR STILL EXISTS

#ifdef __cplusplus
#include <iostream>
#endif

#ifdef __cplusplus
extern "C" {
#endif

void Callme(int a);

#ifdef __cplusplus
}
#endif

MY C++ CODE IS A STATIC LIBRARY.

LINK ERROR

1>------ Rebuild All started: Project: CProject, Configuration: Debug Win32 ------
1>  Ccode.c
1>c:\users\ngk\documents\ccg\vsprojects\expapp\cproject\ccode.c(6): warning C4013: 'CallMe' undefined; assuming extern returning int
1>Ccode.obj : error LNK2019: unresolved external symbol _CallMe referenced in function _main
1>C:\Users\ngk\Documents\CCG\VSprojects\ExpApp\Debug\CProject.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • 12
    `` is a C++ header. Remove it from the common header. – Antti Haapala -- Слава Україні Sep 01 '17 at 16:25
  • 9
    You're including ``, a C++ header, from `cppex.h`, which is included from C code. So the C compiler is trying to compile C++ and gets a headache. – Thomas Sep 01 '17 at 16:25
  • 1
    You cannot seriously expect that a C compiler can compile C++ code, right? – Kaveh Vahedipour Sep 01 '17 at 16:26
  • What if my C++ header is having a bunch of C++ header files like , etc.. without which the C++ library would not function. If i want to use this library in a C code, instead of creating a separate header file specifically for C can I include __cplusplus for header files also. – codeLover Sep 01 '17 at 16:32
  • 2
    @codeLover You should not include headers in a header file in general. Specifically in this case it's causing a lot of trouble. Also, in this case **you don't need to include ** in the header file. Keep in header files only, function declarations, `typedef`s and perhaps sometimes `struct` definitions, if you need a type from another header file and it's for a pointer parameter or structure member, just use a forward declaration. – Iharob Al Asimi Sep 01 '17 at 16:35
  • 1
    What is your linker error? I don’t see it in the copied list of errors. – Daniel H Sep 01 '17 at 16:47
  • @IharobAlAsimi What if I want to declare a `struct` that contains another `struct`, or to write an `inline` function? That advice seems like it might work fine in many cases for C, but it sounds like terrible advice for C++ headers (which often contain more code) and like it can come back to bite you even in C. I get why you wouldn’t include headers which are only needed as part of the implementation, but if a header defines something that’s part of your interface, why *not* include it? – Daniel H Sep 01 '17 at 16:50
  • @codeLover Show us the compiler calls for your translation units. – user0042 Sep 01 '17 at 16:51
  • 4
    In `main` you call `CallMe`, but your function is named `Callme`. The case of the `m` is different. – Daniel H Sep 01 '17 at 16:54
  • 1
    @codeLover Don't make your question a _"moving target"_ please. Be sure to provide a [MCVE] from the beginning. – user0042 Sep 01 '17 at 17:36
  • @DanielH One of the reasons I don't like c++ is *templates* which require the definition of a function to be in a header file. Also, that was just a comment. There are also reasons to include headers inside headers in [tag:c], but I normally avoid it unless it's unavoidable. – Iharob Al Asimi Sep 01 '17 at 17:50
  • @IharobAlAsimi Even without templates making you include things in header files, it seems like a bad idea to re-declare everything. The more times you type something, the more times you’ll mistype it, and the more places you have to change if you want to change it. If header files should just be declarations, then including them is cheap, and it reduces code duplication. My question was genuine: what is a reason not to use include statements in header files? – Daniel H Sep 01 '17 at 18:00
  • the posted code is telling the compiler that the C++ function: `CallMe()` is a C function. I.E. – user3629249 Sep 04 '17 at 19:29
  • The posted code is not your actual code. Other wise, the compile for this statement: `retun 1;` would have failed. Please post a [mcve] – user3629249 Sep 04 '17 at 19:30

5 Answers5

8

Move

#include <iostream>

into the c++ source file.

The C compiler cannot deal with the C++ code in this header if it's included from the main() translation unit.

Also take care to use the exact same function names and parameter types for declaration, definition and calling. Both compilers are case sensitive.

CallMe(int) isn't the same as Callme(int).


As for your edit:

Make sure you add the static library correctly to your project.

user0042
  • 7,917
  • 3
  • 24
  • 39
  • 2
    You could give some detail as to why this solves the problem. – Iharob Al Asimi Sep 01 '17 at 16:38
  • @IharobAlAsimi Better? – user0042 Sep 01 '17 at 16:40
  • I could solve the compilation issue but getting the link error as unresolved external symbol as i've edite din my question version2. – codeLover Sep 01 '17 at 16:49
  • 1
    @codeLover The `extern "C"` declaration should prevent you from name mangling. How are you linking your translation unit compiler results. Add that to your question please. Also you should really move the include statement out of the header as I suggested. – user0042 Sep 01 '17 at 16:53
  • @user0042: I didn't get you when you say - "How are you linking your translation unit compiler results. ". I'm linking STATICALLY as my C++ code is a static library & my C code is EXE. – codeLover Sep 01 '17 at 17:16
  • @codeLover I meant you should show us the linker command, but it turned out you have a typo in your `main()`. – user0042 Sep 01 '17 at 17:18
  • @user0042: Actually there is no typo in my code, but while typing the same in SO i made this typo. However, I'm still getting the link error: error LNK2019: unresolved external symbol _Callme referenced in function _main. As you can see (also in my edited question), that an _(underscore) has been added to teh Callme(). How is that. I'm using the default linker command that comes with VS2013 when we create an empty project. Along with that I'm just adding the path to the C++ lib file. – codeLover Sep 01 '17 at 17:22
  • @codeLover Just adding the linker search path isn't enough. Add the library also to be linked with your project. – user0042 Sep 01 '17 at 17:24
  • I think the linker problem is that `Callme` and `CallMe` are different function names because it’s case sensitive. – Daniel H Sep 01 '17 at 17:55
  • @Daniel OP already clarified that this was fixed. Also my answer explains that. – user0042 Sep 01 '17 at 17:57
  • @user0042 There are a lot of things that “there is no typo” could have meant are not present. I thought there could have been a different one which was introduced in copying, because the casing issue is still present in the question. – Daniel H Sep 01 '17 at 17:59
1

I've had the lovely misfortune of having to combine a main C program calling homegrown C++.

As for your first problem (as others have stated), move the #include <iostream> into your C++ file.

As for your Linker problem:

This may (or may not) be a similar thing that had me spinning my wheels. But it's something to keep in mind because it's insanely silly.

Assuming you are building your C++ code in a DLL, and separately compiling your C code in an exe that calls the DLL library:

In Visual Studio's Unmanaged C, the DLL's don't get cleaned out of the "Release" folder the way they do in Managed C++. You must physically move your new DLL into the "Release" folder of your application program each time you make changes to your DLL.

I learned how to deal with updating DLL's in Managed C++ where all you have to do (in your application program folder) is clear out your old DLL "library subfolder" and copy in the new Release of the changed DLL's into that empty "library subfolder". When you "Clean and Rebuild" the main application that calls those DLL's, the Visual Studio Managed C++ will delete the old DLL's out of the "Release" folder and copy the new DLL's from the "library subfolder" into the applications "Release" folder. It nicely keeps your DLL's up to date.

In Unmanaged C, the "Clean and Rebuild" does not delete your old DLL's out of the application's "Release" folder. And it doesn't copy the new DLL's over from your "library subfolder" either. So even though I thought I was placing my new DLL's in a folder that my program could find them, they were not being copied like they are under Managed C++.

This basically means that any new changes you are making to your C++ DLL... if you are only copying them over to a "library subfolder" in your C project, the new improved code won't be copied to the Release folder like managed C++ does. You also have to copy those new DLL's over to the Release folder.

Which also means... if you had your initial DLL compiled and linked and later added the Callme() function, if you didn't physically copy that DLL to your Release folder, it'll never see your new code. It'll still be looking at your old version and will give you the error your are now seeing.

Here are some tools that can help you track down the dates of the versions your programs are really trying to call. These will let you know if your linker is grabbing an outdated version of your code:

link /dump /exports CPPSource.dll | grep subCreate

link /dump /exports CSource.exe | grep subCreate

Putting your correct file names in for the .dll and .exe

If you are not using DLL's for your C++ library, you can kindly ignore my stab at a solution for you. (Or you can consider using DLL's for your pure C++ code and call/link to it in your pure C application/project.) I've found keeping C++ code completely separate in a DLL from the main C code project is a way to save you from many headaches. (Especially if you are mixing unmanaged code and managed code.)

Hope that helps :)

abraxascarab
  • 661
  • 1
  • 6
  • 13
0

Your C++ function needs to be marked as extern "C" to avoid name mangling and follow C calling convention. If you don't do that it won't be (easily) callable from C.

The above also means you can't use default argument values nor overloading (and more) for such functions that need to be callable from C.

And you need to compile the C++ code with a C++ compiler, of course.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

Simply a stupid Typo! You're declaring and defining Callme() and calling CallMe()

user5329483
  • 1,260
  • 7
  • 11
  • @user0042 Did you mean the `#include ` stuff? I considered this as fixed with the given linker output, which pointed me to the typo. – user5329483 Sep 01 '17 at 17:06
  • 2
    Of course I meant that. Also implying the OP being _stupid_ isn't very nice. And write complete answers. – user0042 Sep 01 '17 at 17:07
  • 2
    And typos should be voted to close the question, because then the answer is only useful for the specific case and only as long as there's a typo. – Iharob Al Asimi Sep 01 '17 at 17:52
-1

You cannot compile C++ code using a C compiler.

Kaveh Vahedipour
  • 3,412
  • 1
  • 14
  • 22