Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从C++;到C_C++_C_Windows - Fatal编程技术网

C++ 从C++;到C

C++ 从C++;到C,c++,c,windows,C++,C,Windows,可能重复: 编辑: 我将库的源代码重新编译为C,这就解决了它 我有这个代码需要在我的应用程序中使用。它是用于编写串口的,我不能理解如何在C中运行它。我在C++中有一个版本,以及一个看起来更像C的版本,它被设计成用Borland C++ 5.5编译器编译,但是我不能在那里编译它或在我的项目中。 编辑:我应该注意到,编译时,它编译(和链接)为C++,而不是编译为C.< /P> 以下是我得到的链接器错误: 1>InpoutTest.obj : error LNK2019: unresolved

可能重复:

编辑:

我将库的源代码重新编译为C,这就解决了它

我有这个代码需要在我的应用程序中使用。它是用于编写串口的,我不能理解如何在C中运行它。我在C++中有一个版本,以及一个看起来更像C的版本,它被设计成用Borland C++ 5.5编译器编译,但是我不能在那里编译它或在我的项目中。 <>编辑:我应该注意到,编译时,它编译(和链接)为C++,而不是编译为C.< /P> 以下是我得到的链接器错误:

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++代码。我不需要命令行功能,我只需要能够调用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;
}
#包括“stdafx.h”
#包括“stdio.h”
#包括“string.h”
#包括“stdlib.h”
/*----Inp和Outp的原型--*/
短_stdcallinp32(短端口地址);
void _stdcallout32(短端口地址、短数据);
/*--------------------------------*/
int main(int argc,char*argv[])
{
int数据;

如果(argc此代码看起来像是在读取/写入打印机端口,而不是串行端口。它是通过加载一些允许直接访问硬件的DLL来实现的。要使其工作,您需要使用相同的(或类似的)DLL从外观上看,它正在使用,因此获取它可能会有所帮助。

您可能需要将Inp32/Outp32的原型包装为“extern C{/*…*/}”我必须重新编译库,然后使用那个版本。现有版本编译为C++。< /p>你得到了什么编译器错误?这两种错误看起来都很像直C。问题可能是你的C编译器不理解杰夫的关键字。你用什么编译器?你得到了什么?顺便说一句,这两个程序之间的主要区别是第一个链接显式地(在链接时)链接到DLL,而第二个使用LoadLibrary在运行时加载库。我使用的是Visual Studio 2008 Express,所以Microsoft编译器。它给了我链接器错误:错误LNK2019:未解析的外部符号_Out32@8函数_setRelaySwitchesAlso中引用的,我指的是打印机/并行端口,而不是串行端口。捕捉得好。这可能会感觉到如果编译成C++而不是C编译,那就是问题所在。OOP,对不起,我的错误……正如MartinB指出的,C版本使用LoadLibrary;你是链接到iPoTu32.DLL还是IpOut32.LIB?我的理解是你需要链接到导入库(in poput33.LIB)。这样链接器就可以解析Inp32和Outp32,同时将实现推迟到input32.dll。我在链接器的命令行选项中添加了input32.lib。这是错误的吗?是否需要采取其他步骤来实现这一点?
#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;
}