C code compiles as C ++, but not as C

Possible duplicate:
Convert code from C ++ to C

I have code that seems straightforward C. When I tell the compiler (I use Visual Studio 2008 Express) to compile it as C ++, it compiles and binds in order. When I try to compile it as C, it causes this error:

1>InpoutTest.obj : error LNK2019: unresolved external symbol _Out32@8 referenced in function _main 1>InpoutTest.obj : error LNK2019: unresolved external symbol _Inp32@4 referenced in function _main 

The code reads and writes to the parallel port using Inpout.dll. I have both Inpout.lib and Inpout.dll. Here is the code:

 // InpoutTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdio.h" #include "string.h" #include "stdlib.h" /* ----Prototypes of Inp and Outp--- */ short _stdcall Inp32(short PortAddress); void _stdcall Out32(short PortAddress, short data); /*--------------------------------*/ int main(int argc, char* argv[]) { int data; if(argc<3) { //too few command line arguments, show usage printf("Error : too few arguments\n\n***** Usage *****\n\nInpoutTest read <ADDRESS> \nor \nInpoutTest write <ADDRESS> <DATA>\n\n\n\n\n"); } else if(!strcmp(argv[1],"read")) { data = Inp32(atoi(argv[2])); printf("Data read from address %s is %d \n\n\n\n",argv[2],data); } else if(!strcmp(argv[1],"write")) { if(argc<4) { printf("Error in arguments supplied"); printf("\n***** Usage *****\n\nInpoutTest read <ADDRESS> \nor \nInpoutTest write <ADDRESS> <DATA>\n\n\n\n\n"); } else { Out32(atoi(argv[2]),atoi(argv[3])); printf("data written to %s\n\n\n",argv[2]); } } return 0; } 

I previously asked this question wrong here .

Any help would be appreciated.

+4
source share
4 answers

You are trying to connect to a C ++ function, starting with C. This does not work due to name enumeration - the linker does not know where to look for your function. If you want to call the C function from C ++, you must mark it extern "C". As far as I know, C does not support extern "C ++". One of the other answers says yes. Alternatively, recompile the source code as C.

Edit: Why don't you compile C if you could compile as C ++?

+6
source

It seems that Inp32 and Out32 defined externally in the C ++ file / library, so you need to mark them as such so that the compiler knows how their names will be distorted:

 extern "C++" { short _stdcall Inp32(short PortAddress); void _stdcall Out32(short PortAddress, short data); } 
+5
source

If you need to call a C ++ subroutine from C code, then for a C ++ subroutine you must have a "C" link, which is executed by marking the function as extern "C" . This needs to be done on the C ++ side.

Put the following prototypes for Inp32() and Outp32() if you can change existing C ++ code. This should be in the header, which included any calls or defined the functions Inp32() or Outp32() - whether it be C or C ++ code:

 #ifdef __cplusplus extern "C" { #endif short _stdcall Inp32(short PortAddress); void _stdcall Out32(short PortAddress, short data); #ifdef __cplusplus } #endif 

This will mean that these functions have a C calling convention, and these functions will be called using C or C ++ code.

If you cannot change the code in C ++, you can create your own C-compatible wrappers for C ++ functions in your own C ++ module:

Header wrappers.h file:

 // in wrappers.h // C-callable wrappers #ifndef WRAPPERS_H #define WRAPPERS_H #ifdef __cplusplus extern "C" { #endif short Inp32_wrapper( short PortAddress); void Out32_wrapper( short PortAddress, short data); #ifdef __cplusplus } #endif #endif /* WRAPPERS_H */ 

And, implementation of wrappers.cpp:

 // in wrappers.cpp file: #include "wrappers.h" // prototypes for the C++ functions - these really should be in a // header file... short _stdcall Inp32(short PortAddress); void _stdcall Out32(short PortAddress, short data); // implementation of the wrappers short Inp32_wrapper( short PortAddress) { return Inp32( PortAddress); } void Out32_wrapper( short PortAddress, short data) { Out32( PortAddress, data); } 

Now your C code can #include "wrappers.h" and call wrapper functions that simply call existing C ++ functions to do the job.

+5
source

This is not a compiler error, but rather a linker error. The linker cannot find the definitions of Inp32 and Out32 . Are you linking to a library containing definitions? Did you write them right?

+2
source

Source: https://habr.com/ru/post/1314231/


All Articles