头文件中包含函数
Inluding functions in header file
我有头文件:
dictionary.h:
#ifndef dictionary_h__
#define dictionary_h__
extern char *BoyerMoore_positive(char *string, int strLength);
extern char *BoyerMoore_negative(char *string, int strLength);
extern char *BoyerMoore_skip(char *string, int strLength);
#endif
函数定义:dictionary.cpp
#include<stdio.h>
#include<string.h>
char *BoyerMoore_positive(char *string, int strLength)
{
} ---- //for each function
和主文件main.cpp:
#include "dictionary.h"
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
void *SocketHandler(void *);
int main(int argv, char **argc)
{
----
skp = BoyerMoore_skip(ch[i], strlen(ch[i]) );
if(skp != NULL)
{
i++;
printf("inn");
continue;
}
printf("n hi2 n");
str = BoyerMoore_positive(ch[i], strlen(ch[i]) );
str2= BoyerMoore_negative(ch[i], strlen(ch[i]) );
----
}
当我执行main.cpp 时
它给出:
/tmp/ccNxb1ix.o: In function `SocketHandler(void*)':
LinServer.cpp:(.text+0x524): undefined reference to `BoyerMoore_skip(char*, int)'
LinServer.cpp:(.text+0x587): undefined reference to `BoyerMoore_positive(char*, int)'
LinServer.cpp:(.text+0x5bd): undefined reference to `BoyerMoore_negative(char*, int)'
collect2: error: ld returned 1 exit status
我不知道为什么它找不到函数!感谢帮助!
您需要将两个源文件编译为main.o
和dictionary.o
,然后将这些对象文件链接到最终可执行文件中:
$ g++ -c main.cpp
$ g++ -c dictionary.cpp
$ g++ -o myexe main.o dictionary.o
或者你可以一次性构建和链接:
$ g++ -o myexe main.cpp dictionary.cpp
你通常会创建一个Makefile
来减轻这个过程的繁琐,它可能只有(未经测试的):
myexe: main.o dictionary.o
那么简单来说:
$ make
您确定您的dictionary.cpp
包含在您的项目中并且构建时没有错误吗?编译后,Linker在对象文件中找不到这些函数,请查看完整的日志以了解dictionary.cpp
文件的编译错误或成功。
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- g++ 说函数不存在,即使包含正确的标头
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 如果基类包含双指针成员,则派生类的构造函数
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 复制包含C++所有元素的对象!(构造函数和赋值,最佳实践?
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 如果 x.h 仅由函数声明组成,为什么有必要在 x 中包含 x.h.cpp
- 如果函数包含静态变量,为什么编译器不执行内联?
- 为什么宏__STL_FUNCTION_MPL_PARTIAL_ORDER应该将模板函数包含在std_pair.h中
- 如果函数包含在命名空间中,则"go to beginning of the function"不起作用 (C++)
- 创建自己所需的函数比为某个函数包含整个库更可取吗
- cv::meanShift函数包含什么
- 是否应将析构函数包含在使用自动存储的指针中
- 函数包含未命名参数
- 如何使我的resize函数包含前一个向量的元素?
- 将头文件中的特定函数包含到c++代码中
- c++类的析构函数包含ptr到对象的数组