C $ C $ç编译为C ++,但不是为C [英] C code compiles as C++, but not as C

查看:155
本文介绍了C $ C $ç编译为C ++,但不是为C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  从C ++转换一些code到C

我有一个看起来是直的C.当我告诉编译器(我使用Visual Studio 2008的前preSS)为C ++,它编译和链接罚款编译一些code。当我尝试编译为C,虽然,它抛出这个错误:

I've got some code that appears to be straight C. When I tell the compiler (I'm using Visual Studio 2008 Express) to compile it as c++, it compiles and links fine. When I try to compile it as C, though, it throws this error:

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

在code读取和写入并行端口,使用Inpout.dll。我都Inpout.lib和Inpout.dll。这里的code:

The code reads from and writes to the parallel port, using Inpout.dll. I have both Inpout.lib and Inpout.dll. Here's the code:

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

我previously问到这个问题,不当的这里

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

您正试图链接到一个C ++函数,从C.这并不工作,由于名称mangling-链接器不知道去哪里找为你的功能。如果你想调用从C ++ C函数,则必须将其标记为externC。 C不支持的externC ++ - 因为据我所知。一个其他的答案说,有。另外,重新编译它的源$ C ​​$ C为C

You're trying to link to a C++ function, from C. That doesn't work due to name mangling- the linker doesn't know where to look for your function. If you want to call a C function from C++, you must mark it extern "C". C does not support extern "C++"- as far as I know. One of the other answers says there is. Alternatively, recompile it's source code as C.

编辑:为什么你永远会编译为C,如果你能为编译C ++,反正

Why ever would you compile as C if you could compile as C++, anyway?

这篇关于C $ C $ç编译为C ++,但不是为C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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