2010-06-29 57 views
4

Possible Duplicate:
Convert some code from C++ to CC代碼編譯爲C++,但不是爲C

我有一個看起來是直的。當我告訴編譯器一些代碼(我使用Visual Studio 2008 Express的)編譯它作爲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 

的代碼讀取和寫入並行端口,使用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; 
} 

我以前問這個問題,不正確,here

任何幫助,將不勝感激。

+0

這些天C是否會使用錯位的名字?我正在查看鏈接器錯誤中的「@ 4」和「@ 8」。看起來不像C++的名稱,除非我很困惑,但那些肯定不是未修飾的函數名稱。 – Steve314 2010-06-29 17:19:22

+2

這只是你以前的問題的一個小改寫:http://stackoverflow.com/questions/3142420/convert-some-code-from-c-to-c。儘量不要加倍張貼。 – 2010-06-29 17:21:01

+0

@Evan:我再次問,因爲我沒有得到新的問題。抱歉。 – 2010-06-29 17:23:51

回答

6

您正在嘗試鏈接到C++函數,這是因爲名稱修改 - 鏈接器不知道在哪裏查找函數。如果您想從C++調用C函數,則必須將其標記爲extern「C」。 C不支持extern「C++」 - 據我所知。其他答案之一說有。或者,將它的源代碼重新編譯爲C.

編輯:如果可以編譯爲C++,爲什麼要編譯爲C?

+0

我需要從C項目調用Out32,如果我嘗試編譯爲C++,會拋出一堆錯誤。 – 2010-06-29 17:43:03

+0

我將lib的源代碼重新編譯爲C,並且工作正常。 (在我將afxres.h的引用更改爲winres.h後) – 2010-06-29 17:48:07

5

這聽起來像Inp32Out32在一個C++文件/庫外部定義的,所以你需要將它們標記爲這樣所以編譯器知道他們的名字如何,將被截斷:

extern "C++" { 
    short _stdcall Inp32(short PortAddress); 
    void _stdcall Out32(short PortAddress, short data); 
} 
+0

這給了錯誤C2059:語法錯誤:'字符串' – 2010-06-29 17:19:41

+0

是否有'extern「C++」'?它可能是不可移植的?我會在頭文件中使用'extern「C'',在那些函數被壓縮的頭文件中,使用'#ifdef'來保護,這樣只有C++編譯器才能看到它。 C編譯器已經使用C調用約定。 – Steve314 2010-06-29 17:25:25

+0

@Steve雖然以另一種方式做這件事更爲常見,我不確定可移植性,因爲我從來沒有真正需要使用它。如果它是一個庫,他不控制/有源碼,但他沒有多少選擇,儘管 – 2010-06-29 17:30:18

2

它不似乎是一個編譯器錯誤,而是一個鏈接器錯誤。鏈接器找不到Inp32Out32的定義。你鏈接到包含定義的庫嗎?你拼寫正確嗎?

+0

我所做的唯一的事情是將文件的設置更改爲C而不是C++進行編譯。我沒有改變任何鏈接或任何東西。有什麼我需要改變鏈接器設置? – 2010-06-29 17:21:35

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 

這將標誌着那些用作具有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 */ 

而且,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); 
} 

現在你的C代碼可以#include "wrappers.h"並調用封裝功能,這只是調用EXI蜇C++函數來完成這項工作。