Convert code from C ++ to C

Possible duplicate:
C code compiles as C ++, but not as C

Edit:

I recompiled the source for the library as C and fixed it.

I have this code that I need to use in my application. This is for writing to the serial port, and I can’t figure out how to make it work in C. I have a version in C ++, as well as a version that is more like C, designed to compile with Borland C ++ 5.5, but I cannot compile it there or in my project.

Edit: I have to note that it compiles (and links) when compiling as C ++, but not when I compile as c.

Here is the linker 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 

Here is the C ++ code. I don't need command line functionality, I just need to be able to call Out32 (). I don’t even have to read.

 #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; } 

Here is another example:

 #include <stdio.h> #include <conio.h> #include <windows.h> /* Definitions in the build of inpout32.dll are: */ /* short _stdcall Inp32(short PortAddress); */ /* void _stdcall Out32(short PortAddress, short data); */ /* prototype (function typedef) for DLL function Inp32: */ typedef short _stdcall (*inpfuncPtr)(short portaddr); typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum); int main(void) { HINSTANCE hLib; inpfuncPtr inp32; oupfuncPtr oup32; short x; int i; /* Load the library */ hLib = LoadLibrary("inpout32.dll"); if (hLib == NULL) { printf("LoadLibrary Failed.\n"); return -1; } /* get the address of the function */ inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32"); if (inp32 == NULL) { printf("GetProcAddress for Inp32 Failed.\n"); return -1; } oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32"); if (oup32 == NULL) { printf("GetProcAddress for Oup32 Failed.\n"); return -1; } /***************************************************************/ /* now test the functions */ /* Try to read 0x378..0x37F, LPT1: */ for (i=0x378; (i<0x380); i++) { x = (inp32)(i); printf("port read (%04X)= %04X\n",i,x); } /***** Write the data register */ i=0x378; x=0x77; (oup32)(i,x); printf("port write to 0x%X, datum=0x%2X\n" ,i ,x); /***** And read back to verify */ x = (inp32)(i); printf("port read (%04X)= %04X\n",i,x); /***** One more time, different value */ i=0x378; x=0xAA; (oup32)(i,x); printf("port write to 0x%X, datum=0x%2X\n" ,i ,x); /***** And read back to verify */ x = (inp32)(i); printf("port read (%04X)= %04X\n",i,x); FreeLibrary(hLib); return 0; } 

Any help would be appreciated.

0
source share
3 answers

I had to recompile the library as C, and then use this version. The existing version was compiled as C ++.

0
source

You may need to wrap the Inp32 / Outp32 prototypes with "extern C {/ * ... * /}".

+1
source

This code looks like it is reading / writing a printer port, not a serial port. He does this by loading some DLL that gives direct access to the hardware. To make it work, you will need to use the same (or similar) DLL. From the looks of things, it uses Inpout32.dll , so getting this can be useful.

0
source

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


All Articles