c++工程中如何加入c文件?

delinpeng 2002-12-05 10:53:24
...is not a precompiled header file created with this compiler

我在一个mfc的工程里想加入一些别人写好的c文件,可编译时会出现上面描述的错误
...全文
460 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
delinpeng 2002-12-10
  • 打赏
  • 举报
回复
我最终是做成了lib文件。
大家对此问题还有没有其他的想法?
要不我只有结贴了。
delinpeng 2002-12-05
  • 打赏
  • 举报
回复
qing_li73(bluemoon) :谢谢提醒,我的英文水平不行,平时很少看msdn,看来是该补补课了。
studentforever:不行,这样做又会出现其他的错误。
archoo(archoo) :不行,如果我将该c文件所有行都删掉,依然会出现这个错误。
谢谢楼上诸位的发言。
zhou80bin 2002-12-05
  • 打赏
  • 举报
回复
up
archoo 2002-12-05
  • 打赏
  • 举报
回复
c函数编译出来的代码和C++不同;

你将整个.c文件中的所有函数都用
extern "C" {
...
}
应该就可以了;
studentforever 2002-12-05
  • 打赏
  • 举报
回复
project,settings,c++标签中,选中precompiled headers,选中not using precompiled headers
delinpeng 2002-12-05
  • 打赏
  • 举报
回复
问题在于别人的文件是.c文件,我原来以为将文件名改为.cpp就可以了,没想到还是报错。
听同事讲是在某个地方写上一句 extern "c++",可我就是试不对
qing_li73 2002-12-05
  • 打赏
  • 举报
回复
Just paste a snippet from MSDN ,u can find more by yourself :

Linkage Specifications
The term “linkage specification” refers to the protocol for linking functions (or procedures) written in different languages. The following calling conventions are affected:

Case sensitivity of names.


Decoration of names. In C, the compiler prefixes names with an underscore. This is often called “decoration.” In C++, name decoration is used to retain type information through the linkage phase. (SeeDecorated Names, in Visual C++ Programmer’s Guide.)


Order in which arguments are expected on the stack.


Responsibility for adjusting the stack on function return. Either the called function or the calling function is responsible.


Passing of hidden arguments (whether any hidden arguments are passed).
Syntax

linkage-specification :

extern string-literal { declaration-listopt }
extern string-literal declaration

declaration-list :

declaration
declaration-list

Linkage specification facilitates gradually porting C code to C++ by allowing the use of existing code.

Microsoft Specific

The only linkage specifications currently supported by Microsoft C++ are "C" and "C++".

END Microsoft Specific

The following example declares the functions atoi and atol with C linkage:

extern "C"
{
int atoi( char *string );
long atol( char *string );
}

Calls to these functions are made using C linkage. The same result could be achieved with these two declarations:

extern "C" int atoi( char *string );
extern "C" long atol( char *string );

Microsoft Specific

All Microsoft C standard include files use conditional compilation directives to detect C++ compilation. When a C++ compilation is detected, the prototypes are enclosed in an extern "C" directive as follows:

// Sample.h
#if defined(__cplusplus)
extern "C"
{
#endif

// Function declarations

#if defined(__cplusplus)
}
#endif

END Microsoft Specific

You do not need to declare the functions in the standard include files as extern "C".

If a function is overloaded, no more than one of the functions of the same name can have a linkage specifier. (For more information, see Function Overloading in Chapter 7.)

Table 6.3 shows how various linkage specifications work.

Table 6.3 Effects of Linkage Specifications

Specification Effect
On an object Affects linkage of that object only
On a function Affects linkage of that function and all functions or objects declared within it
On a class Affects linkage of all nonmember functions and objects declared within the class


If a function has more than one linkage specification, they must agree; it is an error to declare functions as having both C and C++ linkage. Furthermore, if two declarations for a function occur in a program — one with a linkage specification and one without — the declaration with the linkage specification must be first. Any redundant declarations of functions that already have linkage specification are given the linkage specified in the first declaration. For example:

extern "C" int CFunc1();
...
int CFunc1(); // Redeclaration is benign; C linkage is
// retained.

int CFunc2();
...
extern "C" int CFunc2(); // Error: not the first declaration of
// CFunc2; cannot contain linkage
// specifier.

Functions and objects explicitly declared as static within the body of a compound linkage specifier ({ }) are treated as static functions or objects; the linkage specifier is ignored. Other functions and objects behave as if declared using the extern keyword. (See Storage-Class Specifiers for details about the extern keyword.)
ruihuahan 2002-12-05
  • 打赏
  • 举报
回复
只加入源码(.h、.cpp),不要加入其它编译器产生的任何东西。应该没有问题,C++是C的超集嘛。
esc123 2002-12-05
  • 打赏
  • 举报
回复
在你需要引用该C文件的头文件的地方加入
extern "C"
{
#include "头文件";
}
试试!
delinpeng 2002-12-05
  • 打赏
  • 举报
回复
还有谁发表意见?

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧