简体   繁体   English

使用我自己的库:隐式声明函数

[英]Using my own library : implicit declaration of function

Firstly, I'd like to thanks you in advance for the time you'll take to help me out. 首先,我想提前感谢你的时间来帮助我。 If I may suggest you, you can try reproduce my problem. 如果我建议你,你可以尝试重现我的问题。 Don't try to read the makefiles if you don't feel it'll help you to understand my problem. 如果您觉得它不能帮助您理解我的问题,请不要尝试阅读makefile。

Also, I'd like to point out the fact I did a lot of researches and I don't have find any solution. 另外,我想指出我做了很多研究的事实,我没有找到任何解决方案。

My Environment 我的环境


  • Eclipse (with CDT) Eclipse(带CDT)
  • Windows (cygwin) (but I also tried on Ubuntu) Windows(cygwin) (但我也试过Ubuntu)

I want to use my own (shared) library in a project. 我想在项目中使用我自己的(共享)库。

My Setup 我的设置


My Shared Library 我的共享图书馆

mylib.h mylib.h

#ifndef MYLIB_H_
#define MYLIB_H_

extern int foo();

#endif /* MYLIB_H_ */

mylib.c mylib.c

#include "mylib.h"

extern int foo() {
    return 1;
}

My Project 我的项目

I added my library as a reference : 我添加了我的库作为参考:

Project Properties - C/C Generals - Paths and Symbols - References (tab) - Check off mylib (Active) 项目属性 - C / C将军 - 路径和符号 - 参考(选项卡) - 选中mylib(活动)

foo.c foo.c的

#include <stdlib.h>

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

Problem 问题


I'm getting implicit declaration of function 'foo' [-Wimplicit-function-declaration] warning when I build my project. 我在构建项目时收到函数'foo'[-Wimplicit-function-declaration]警告的隐式声明 This warning only occurs when I build my project while my library project has nothing to build (because it hasn't been modified since the last build). 这个警告只发生在我构建我的项目而我的库项目没有任何东西要构建时(因为它自上次构建以来没有被修改)。

Console output 控制台输出

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 

Should I provide eclipse my own makefiles ? 我应该提供eclipse自己的makefile吗? (under C/C++ / Builder Settings) (在C / C ++ / Builder设置下)

Solution


I had to include my header in foo.c 我必须在foo.c中包含我的标题

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

The path is kind of weird because of my projects structures : 由于我的项目结构,这条路有点奇怪:

  • myproject 我的项目

    • src SRC
      • foo.c foo.c的
  • mylib MYLIB

    • src SRC
      • mylib.c mylib.c
      • mylib.h mylib.h

Thanks to user590028 for helping me getting through that ! 感谢user590028帮助我完成了这一切!

In foo.c you forgot to include the mylib.h header 在foo.c中,你忘了包含mylib.h头文件

/* foo.c */

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

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

You should include 你应该包括

extern int foo();

in foo.c 在foo.c

then you can compile: 然后你可以编译:

gcc -c mylib.c
gcc mylib.o foo.c -o foo

and execute: 并执行:

./foo

As you are using eclipse, maybe it compiles corret after including the extern line and it's not needed to compile manually. 当您使用eclipse时,也许它在包含extern行后编译相关,并且不需要手动编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM