最近我学会了C中的隐式函数声明.主要观点很明确,但我对在这种情况下理解链接过程有些麻烦.
请考虑以下代码(文件ac):
#include <stdio.h>
int main() {
double someValue = f();
printf("%f\n", someValue);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试编译它:
gcc -c a.c -std=c99
Run Code Online (Sandbox Code Playgroud)
我看到关于隐式声明功能的警告f().
如果我尝试编译和链接:
gcc a.c -std=c99
Run Code Online (Sandbox Code Playgroud)
我有一个未定义的引用错误.一切都很好.
然后我添加另一个文件(文件bc):
double f(double x) {
return x;
}
Run Code Online (Sandbox Code Playgroud)
并调用下一个命令:
gcc a.c b.c -std=c99
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,一切都成功连接 当然在./a.out调用后我看到了垃圾输出.
所以,我的问题是:如何将隐式声明函数的程序链接起来?在编译器/链接器的引擎下我的例子会发生什么?
我有众所周知的错误:
implicit declaration of function 'STLINKReadSytemCalls' [-Wimplicit-function-declaration]
implicit declaration of function 'printf' [-Wimplicit-function-declaration]
incompatible implicit declaration of built-in function 'printf'
Eclipse(更准确地说是 Atollic TrueStudio)友好地补充道:
include '<stdio.h>' or provide a declaration of 'printf'
阅读了数十亿篇关于如何解决这个问题的帖子,似乎三个问题可能会导致这些错误:
#ifndef,#define并且#endif没有正确包装头文件我发现一个帖子,其中有人似乎有这个错误,并在修复它后说 Eclipse 是问题所在。虽然找不到主题,但他的解决方案对我不起作用。这就像单击函数,source -> addincludes。
主程序
int main(void) {
if (STLINKReadSytemCalls() == 1)
printf("Error in system calls.\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
文件处理.c
#include "../header/fileProcessing.h"
int STLINKReadSytemCalls(void) {
// mainly system calls
}
Run Code Online (Sandbox Code Playgroud)
文件处理.h
#ifndef FILEPROCESSING_H_
#define FILEPROCESSING_H_
#include <stdlib.h> …Run Code Online (Sandbox Code Playgroud) 在声明下面的方法时,XCode向我返回一个警告,说明"函数的隐式声明'completionHandler'在C99中无效",那么问题是什么?
+ (void)searchWeatherForCorrdinate:(CLLocationCoordinate2D)coordinate completionHandler:(void(^)(void))handler
{
completionHandler();
}
Run Code Online (Sandbox Code Playgroud) objective-c compiler-warnings implicit-declaration objective-c-blocks
有人可以向我解释为什么以下编译:
int main()
{
int a = mymethod(0);
}
int mymethod(int b)
{
return b;
}
Run Code Online (Sandbox Code Playgroud)
但这不会:
int main()
{
mymethod(0);
}
void mymethod(int b)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
我认为在 C/C++ 中需要前向声明,但这里有一个反例。隐式声明如何在 C 中工作?
在回答这个问题时,我在Ideone上编译了代码并得到了这个错误
implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud)
是不是stdio.h标题scanf_s?
请考虑以下示例代码:
#include <iostream>
using namespace std;
class core
{
public:
core(const core& obj)
{
cout << "core copy ctor called\n";
}
core()
{
cout << "core default ctor called\n";
}
};
class sample : public core
{
public:
sample()
{
cout << "sample default ctor called\n";
}
#if 0
sample(const sample& obj)
{
cout << "sample copy ctor called\n";
}
#endif
};
int main()
{
sample s1;
sample s2 = s1; //Line1
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Type1:未为类示例显式声明的复制构造函数
(Type1显示在上面的代码中.然后编译器隐式生成类sample的复制构造函数).Line1执行语句时,首先class …
c++ copy-constructor default-constructor implicit-declaration
c ×4
c++ ×1
c11 ×1
eclipse ×1
function ×1
header-files ×1
include-path ×1
linkage ×1
objective-c ×1
tr24731 ×1