gpt4 book ai didi

c++ - 将一些代码从 C++ 转换为 C

转载 作者:行者123 更新时间:2023-11-28 08:28:28 26 4
gpt4 key购买 nike

<分区>

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

编辑:

我将库的源代码重新编译为 C,并修复了它。

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

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

这是我得到的链接器错误:

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

这是另一个示例:

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

如有任何帮助,我们将不胜感激。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com