Thread: multiple source files

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    Question multiple source files

    I am trying to compile with 2 .c files and a .h file

    I have something like this

    recognizer.c
    Code:
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include "recheader.h"
    #define ENDOFINPUT "ENDOFINPUT"
    
    Lexeme* lex();
    
    int main(int argc, char *argv[])
    {	
        int recognizer(char*);
        recognizer(argv[1]);
    }
    
    int recognizer(char* filename)
    {	
    	Lexeme* lexeme;
    	lexInit(filename);
    	lexeme = lex();
    	while(strcmp(lexeme->type, ENDOFINPUT) != 0)
    	{
    		
    		lexemeDisplay(lexeme);
    		lexeme = lex();
    
    	}
    	return 0;
    }
    recheader.h
    Code:
    #ifndef RECHEADER_H
    #define RECHEADER_H
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct lexword
    {
    	char type[15];
    	char value[100];
    	int value_length;
    	struct lexword *next;
    }Lexeme;
    
    typedef struct
    {
    	struct lexword *Head;
    }Header;
    
    void lexInit(char* filename);
    int lexemeDisplay();
    function.c
    Code:
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include "recheader.h"
    
    int lexemeDisplay();
    Lexeme* lex();
    
    void lexInit(char* filename)
    {
    	:
            :
    
    }
    
    Lexeme* lex()
    {
    	:
            :
            :
    }
    int lexemeDisplay()
    {
          :
    }
    The problem is when I compile, I get this message
    [Linker error] undefined reference to `lexInit'
    [Linker error] undefined reference to `lex'
    [Linker error] undefined reference to `lexemeDisplay'
    [Linker error] undefined reference to `lex'

    I realize this is probably trivial to most, but I have read a few tutorials and am still confused.
    Any help would be appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just tell the compiler all the names of your .c files

    gcc recognizer.c function.c

    When you have say more than 4 or 5 files, you'll want to explore the delights of makefiles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    Thanks for your help
    where would I add this line?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What OS/Compiler?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    bloodshed dev-C++, for some reason my program runs fine on microsoft compilers but develops linker errors on dev-c++.

    where do I put the gcc... line on bloodshed though?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    and windows xp sorry

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Right-click on your project and "Add to project"
    Choose all the source files you want added.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple source files in one project
    By mintsmike in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2009, 07:20 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM