C++ 意外的未定义引用

C++ 意外的未定义引用,c++,c,cmake,static-libraries,C++,C,Cmake,Static Libraries,我得到一个未定义的引用错误,不知道原因 所以我有两个文件组成了一个静态库:keyboard\u input.c,keyboard\u input.h 以下是.h文件的内容: #ifndef __MOD_KBINPUT__ #define __MOD_KBINPUT__ int kbInit(); int kbWait(); int kbTest(); #endif CMakeLists.txt文件如下所示: FILE( GLOB_RECURSE sources *.c ) INCLUDE

我得到一个未定义的引用错误,不知道原因

所以我有两个文件组成了一个静态库:keyboard\u input.c,keyboard\u input.h

以下是.h文件的内容:

#ifndef __MOD_KBINPUT__
#define __MOD_KBINPUT__

int kbInit();
int kbWait();

int kbTest();

#endif
CMakeLists.txt文件如下所示:

FILE(
GLOB_RECURSE
sources
*.c
)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
ADD_LIBRARY(keyboardReader ${sources})
编译此库时会给出一些警告:

src/utils/kbreader/keyboard_input.c: In function ‘kbInit’:
src/utils/kbreader/keyboard_input.c:13:14: warning: assignment from incompatible pointer type [enabled by default]
src/utils/kbreader/keyboard_input.c: In function ‘kbWait’:
src/utils/kbreader/keyboard_input.c:21:55: warning: passing argument 4 of ‘fread’ from  incompatible pointer type [enabled by default]
/usr/include/stdio.h:708:15: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘struct FILE *’
现在,对于我的主可执行文件main.cpp:

#include <keyboard_input.h>
int main()
{
  kbTest();
  return 0;
}
最终会出现这样的错误:

CMakeFiles/PEM.dir/main.cpp.o: In function `main':
main.cpp:(.text+0xb): undefined reference to `kbTest()'
collect2: ld returned 1 exit status
make[2]: *** [src/PEM/main2/PEM] Error 1
make[1]: *** [src/PEM/main2/CMakeFiles/PEM.dir/all] Error 2
libkeyboardReader.a被创建,kbTest函数除了

{return 0; }
如果我在头文件中设置kbTest的定义,它就可以工作

但当我输入时,有一点我不明白:make keyboardReader这里是输出:

[ 73%] Building C object src/utils/kbreader/CMakeFiles/KeyboardReader.dir/keyboard_input.c.o
[Warning explained above]
Linking C static library ../../../lib/libKeyboardReader.a

有什么不对劲吗?注释错误消息是否使我的LIB省略了KiBoAdRoPix.c文件?

< p>你正在混合C和C++文件。为了使这一工作,你只需要告诉C++编译器它调用C函数,通过改变头文件,如::/p>
#ifndef MOD_KBINPUT
#define MOD_KBINPUT
/* note I also fixed the macro so you aren't using a system-reserved name */

#if __cplusplus
/* this is the important part */
extern "C" {
#endif

int kbInit();
int kbWait();

int kbTest();

#if __cplusplus
}
#endif

#endif

< C++ >编译器假定函数将给出一个C++内部名称,该代码对签名中的所有类型信息进行编码,这是链接器区分重载函数和链接器没有找到它的原因。

图标和按钮没有显示,我不确定它是否已经标记了您的答案。但是谢谢。我终于了解了那一部分的目的,我感到松了一口气。
#ifndef MOD_KBINPUT
#define MOD_KBINPUT
/* note I also fixed the macro so you aren't using a system-reserved name */

#if __cplusplus
/* this is the important part */
extern "C" {
#endif

int kbInit();
int kbWait();

int kbTest();

#if __cplusplus
}
#endif

#endif