1

I have 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

function definations: dictionary.cpp

#include<stdio.h>
#include<string.h>
char *BoyerMoore_positive(char *string, int strLength)
{
} ---- //for each function

and main file 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("in\n");
            continue;
        }
        printf("\n hi2 \n");
        str = BoyerMoore_positive(ch[i], strlen(ch[i]) );
        str2= BoyerMoore_negative(ch[i], strlen(ch[i]) );
----
}

When I execute main.cpp

it gives:

/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

I dont know why it could not find the function! Help appreciated!

stijn
  • 34,664
  • 13
  • 111
  • 163
user123
  • 5,269
  • 16
  • 73
  • 121

2 Answers2

3

You need to compile both source files into main.o and dictionary.o and then link these object file together into the final executable:

$ g++ -c main.cpp
$ g++ -c dictionary.cpp
$ g++ -o myexe main.o dictionary.o

Or you can build and link in one go:

$ g++ -o myexe main.cpp dictionary.cpp 

You'd normally create a Makefile to take the drudgery out of this process, which might be as little as (untested):

myexe: main.o dictionary.o

Then it's simply:

$ make
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
1

Are you sure that your dictionary.cpp is included to your project and built without errors? Linker can't find those functions in object-files after compilation, check out full log for compilation error or success of your dictionary.cpp file.

Pavlus
  • 1,651
  • 1
  • 13
  • 24