c - 使用我自己的库 : implicit declaration of function

标签 c eclipse makefile shared-libraries multi-project

首先,我想提前感谢您抽出时间来帮助我。如果我可以建议你,你可以尝试重现我的问题。如果您觉得阅读 makefile 不会帮助您理解我的问题,请不要尝试阅读。

此外,我想指出的事实是我做了很多研究,但我没有找到任何解决方案。

我的环境


  • Eclipse(带 CDT)
  • Windows (cygwin)(但我也在 Ubuntu 上试过)

我想在项目中使用我自己的(共享的)库。

我的设置


我的共享库

mylib.h

#ifndef MYLIB_H_
#define MYLIB_H_

extern int foo();

#endif /* MYLIB_H_ */

mylib.c

#include "mylib.h"

extern int foo() {
    return 1;
}

我的项目

我添加了我的图书馆作为引用:

Project Properties - C/C Generals - Paths and Symbols - References (tab) - Check off mylib (Active)

foo.c

#include <stdlib.h>

int main(int argc, char **argv) {
    return foo();
}

问题


当我构建我的项目时,我收到了implicit declaration of function 'foo' [-Wimplicit-function-declaration] 警告。 此警告仅在我构建项目时出现,而我的库项目没有要构建的项目(因为它自上次构建以来未被修改)。

控制台输出

Info: Internal Builder is used for build
gcc -std=c99 "-ID:\\Users\\cmourgeo\\maximo workspace\\mylib" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\uselib.o" "..\\src\\uselib.c" 
..\src\uselib.c: In function 'main':
..\src\uselib.c:12:2: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration]
  return foo();
  ^
gcc "-LD:\\Users\\cmourgeo\\maximo workspace\\mylib\\Debug" -o uselib.exe "src\\uselib.o" -lmylib 

我应该为 eclipse 提供我自己的 makefile 吗? (在 C/C++/Builder 设置下)

解决方案


我必须在 foo.c 中包含我的标题

#include "../src/mylib.h"

由于我的项目结构,路径有点奇怪:

  • 我的项目

    • 来源
      • foo.c
  • 我的库

    • 来源
      • mylib.c
      • mylib.h

感谢 user590028 帮助我度过难关!

最佳答案

在 foo.c 中,您忘记包含 mylib.h header

/* foo.c */

#include <stdlib.h>
#include "mylib.h"  /* <-- include this line */

int main(int argc, char **argv) {
    return foo();
}

关于c - 使用我自己的库 : implicit declaration of function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28371138/

相关文章:

c - C中数组的递归求和

c - C 中 Switch-Case 的意外行为

java - Eclipse 无法运行 - JVM 已终止,Ubuntu 16.04

java - Eclipse IDE 崩溃,因为 Windows 上的 bash.exe 和 which.exe

c - Make 仅运行 'gcc',不带任何参数

c - 如何使用 gcc 和 make 为每个优化级别编译成二进制文件?

java - JNA 和结构体中的 boolean 数组

java - 错误 "A JNI error has occurred. Please check your installation and try again in Eclipse x86 Windows 8.1"

c - lex yacc 和 C 的简单 makefile

c - libgit2:查找两个标签之间的所有提交?