C代码编译为C++,但不是C编译

Col*_*lue 4 c c++ visual-studio

可能重复:
将一些代码从C++转换为C.

我有一些似乎是直接C的代码.当我告诉编译器(我使用Visual Studio 2008 Express)将其编译为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
Run Code Online (Sandbox Code Playgroud)

代码使用Inpout.dll读取和写入并行端口.我有Inpout.lib和Inpout.dll.这是代码:

// InpoutTest.cpp : Defines the entry point for the console application.
//

#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;
}
Run Code Online (Sandbox Code Playgroud)

我以前在这里错误地问过这个问题.

任何帮助,将不胜感激.

Pup*_*ppy 6

您正试图从C链接到C++函数.由于名称修改而无效 - 链接器不知道在哪里查找您的函数.如果要从C++调用C函数,则必须将其标记为extern"C".C不支持extern"C++" - 据我所知.其中一个答案说有.或者,将其源代码重新编译为C.

编辑:如果你可以编译成C++,为什么还要编译为C呢?


Mic*_*zek 5

这听起来像Inp32Out32在一个C++文件/库外部定义的,所以你需要将它们标记为这样所以编译器知道他们的名字如何,将被截断:

extern "C++" {
    short _stdcall Inp32(short PortAddress);
    void _stdcall Out32(short PortAddress, short data);
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*urr 5

如果需要从C代码调用C++例程,那么C++例程需要具有"C"链接,这通过将函数标记为来完成extern "C".这需要在C++方面完成.

如果您能够更改现有的C++代码Inp32(),Outp32()请将以下内容作为原型.这应该是在真实通过任何调用包括或限定的一个首部Inp32()Outp32()功能-无论是C或C++代码:

#ifdef __cplusplus
extern "C" {
#endif

short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

这将把这些函数标记为具有C调用约定,并且这些函数可以通过C或C++代码调用.

如果您无法更改C++代码,则可以在自己的C++模块中为C++函数创建自己的C兼容包装器:

wrappers.h头文件:

// in wrappers.h

// C-callable wrappers

#ifndef WRAPPERS_H
#define WRAPPERS_H

#ifdef __cplusplus
extern "C" {
#endif

short Inp32_wrapper( short PortAddress);
void Out32_wrapper( short PortAddress, short data);    

#ifdef __cplusplus
}
#endif

#endif /* WRAPPERS_H */
Run Code Online (Sandbox Code Playgroud)

而且,wrappers.cpp实现:

// in wrappers.cpp file:

#include "wrappers.h"

// prototypes for the C++ functions - these really should be in a
//  header file...

short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

// implementation of the wrappers

short Inp32_wrapper( short PortAddress)
{
    return Inp32( PortAddress);
}

void Out32_wrapper( short PortAddress, short data)
{
    Out32( PortAddress, data);
}
Run Code Online (Sandbox Code Playgroud)

现在你的C代码可以#include "wrappers.h"调用包装器函数,这些函数只调用现有的C++函数来完成工作.