Thread: Use methods from other C-files

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    46

    Use methods from other C-files

    Hi..

    This question might sound silly, but I am confused and stressed big time..

    I need to acces some functions from another c-file I have.

    Ex. main.c
    Code:
    #include "otherFile.h" // Or how should I do to access the functions in otherFile.c 
    int main() {
    otherFilefunction();
    return 1;
    }
    Ex. otherFile.c
    Code:
    #include "otherFile.h"
    void otherFilefunction(void) {
    // do something
    }
    otherFile.h

    Code:
    void otherFilefunction(void);
    How do i access the functions in the other file? I'm just getting undefined references to the methods I'm using..?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Compile both .c files together.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by OnionKnight View Post
    Compile both .c files together.
    Okay i have a makefile that in this case might look like:
    Code:
    objects = main.o theOtherfile.o
    
    edit : $(objects)
    	cc -lm -o GB-Program $(objects)
    
    main.o : globalVariables.h theOtherfile.h
    theOtherfile.o : theOtherfile.h
    This means that main.o compiles together wiht theOtherFile right..? I still get the error.. :S

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    46
    I have found out that the problem might not be because the file cannot be found because other methods in the files can be used.. The problem consist also in functions in the same file (it cannot load some of it's own methods. The problem seems that the functions don't seem to be known not to the other files as well. I would think that the problem was in the header where the methods is declared propertly, but they exist in the header:
    Code:
    #include "string.h"
    #include <stdio.h>
    #include <math.h>
    #ifndef CONTROL_MODULE_H
    #define CONTROL_MODULE_H
    
    int startupdemons(void);
    int ReadingFromDemons(void);
    int stopdemons(void);
    void lasercontrol(void);
    int control(void);
    void speakcontrol(void);
    void drivecontrol(void);
    void speechrecognitioncontrol(void); 
    void smrdcontrol(void);
    void checkIdle(void);
    void RecogniseWordInSentence(struct sentencestruct);
    
    #endif
    It only three of these that might not be known: startupdemons and checkIdle and RecogniseWordInSentence.

    The error is this:
    [e@sen final]# make
    cc -c -o controlModule.o controlModule.c
    cc -lm -o /shome/ex44/DMZ/final/GB-Program globalVariables.o drive.o staff.o speak.o laser.o controlModule.o client.o speechRecognition.o guiConnector.o
    controlModule.o(.text+0x87f): In function `smrdcontrol.2':
    : undefined reference to `checkIdle'
    controlModule.o(.text+0xd0a): In function `speechrecognitioncontrol.5':
    : undefined reference to `RecogniseWordInSentence'
    guiConnector.o(.text+0xb2): In function `main':
    : undefined reference to `startupdemons'
    collect2: ld returned 1 exit status
    make: *** [edit] Error 1



    BTW. the input to RecogniseWordInSentence is a pointer to the struct but will not allow me to put an * after sentencestruct.. It is called like this
    Code:
    struct sentencestruct speechSentences;
    // ... puts something into the struct ...
    RecogniseWordInSentence(speechSentences);
    Can this vbe a problem..?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by sniper83 View Post
    Code:
    objects = main.o theOtherfile.o
    
    edit : $(objects)
        cc -lm -o GB-Program $(objects)
    
    main.o : globalVariables.h theOtherfile.h
    theOtherfile.o : theOtherfile.h
    you are not listing the source files as dependencies of the object files. Also, you do not say what to do to update the object file dependencies. When compiling to an object file, be sure to use the -c option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM