从C转换一些code ++到C [英] Convert some code from C++ to C

查看:139
本文介绍了从C转换一些code ++到C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
   C $ C $ç编译为C ++,但不为C

编辑:

我重新编译库为C的来源,并固定它。

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

我这有code,我需要在我的应用程序中使用。这对写串口,我无法弄清楚如何获得它在运行C.我有C ++中的版本,还有一个版本,看起来更像C,设计采用了Borland编译C ++编译器5.5,但我不能得到它来编译或出现在我的项目。

I've got this code I need to use in my application. It's for writing to the serial port, and I can't figure out how to get it to run in C. I've got a version in C++, as well as a version that looks more like C, designed to compile with the Borland C++ 5.5 compiler, but I can't get it to compile there or in my project.

编辑:我要指出,它编译(和链接)当我编译为C ++,但不是当我编译为c

I should note that it compiles (and links) when I compile as c++, but not when I compile as c.

下面的链接器错误我得到:

Here's the linker error I get:

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

下面是C ++的code。我并不需要在命令行功能,我只需要能够调用Out32()。我甚至都不需要能够阅读。

Here's the c++ code. I don't need the command line functionality, I just need to be able to call Out32(). I don't even need to be able 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;
}

下面是另一个示例:

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

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

我不得不重新编译库为C,然后使用该版本。现有版本被编译为C ++。

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

这篇关于从C转换一些code ++到C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆