2010-06-29 59 views
0

可能重复:
C code compiles as C++, but not as C转换从C++一些代码至C

编辑:

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

我有我需要在我的应用程序中使用此代码。这是为了写入串行端口,我不知道如何让它在C中运行。我有一个C++版本,以及一个看起来更像C的版本,用于编译Borland C++ 5.5编译器,但我不能让它在那里或在我的项目中编译。

编辑:我应该注意,它编译(和链接)当我编译为c + +,但不是当我编译为c。

这里的链接错误我得到:

1>InpoutTest.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
1>InpoutTest.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 

这里的C++代码。我不需要命令行功能,我只需要能够调用Out32()。我甚至不需要阅读。

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

任何帮助,将不胜感激。

+4

你得到什么编译器错误? – 2010-06-29 15:56:47

+3

这两个看起来都非常像直C语言。问题可能是您的C编译器不理解_stdcall关键字的含义。你使用什么编译器?而且,正如杰夫所问,你得到了什么错误? – 2010-06-29 16:00:38

+0

顺便提一句,这两个程序之间的主要区别在于第一次链接到DLL(在链接时),第二次使用LoadLibrary在运行时加载库。 – 2010-06-29 16:06:39

回答

0

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

0

此代码看起来像是读取/写入打印机端口,而不是串行端口。这是通过加载一些可直接访问硬件的DLL来实现的。为了使它工作,你需要使用相同的(或类似的)DLL。从外观上看,它使用的是Inpout32.dll,所以这可能会有所帮助。

+0

我确实有Inpout32.dll。 – 2010-06-29 16:31:15

+0

另外,我的意思是打印机/并口,而不是串口。接得好。 – 2010-06-29 16:37:04

1

您可能需要用“extern C {/ * ... * /}”来包装Inp32/Outp32的原型。

+0

这可能是有道理的,如果它编译为C++而不是C编译有问题,但它是C编译,这是问题。 – 2010-06-29 16:46:35

+0

哎呀,对不起,我的错误... 正如MartinB指出的那样,C版本使用LoadLibrary;你链接到Inpout32.dll或Inpout32.lib?我的理解是,您需要链接到导入库(Inpout32.lib),以便链接器可以解析Inp32和Outp32转换为* something *,同时将实现推迟到Inpout32.dll。 – Roddy 2010-06-29 17:11:59

+0

我将Inpout32.lib添加到链接器的命令行选项。那是错的吗?为此,我需要采取其他一些措施吗? – 2010-06-29 17:22:42