gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:07 26 4
gpt4 key购买 nike

首先,我想提前感谢您抽出时间来帮助我。如果我可以建议你,你可以尝试重现我的问题。如果您觉得阅读 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com