Thread: (MinGW)I don't know how to link files together

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    (MinGW)I don't know how to link files together

    I tried something like this:

    The main.c
    Code:
    #include <malloc.h>
    #include <stdio.h>
    
    #include "GetLine.h"
    
    int main(void){
        char *Input = (char *)malloc(sizeof(char) * 100);
    
        GetLine(Input, 100)
    
        printf("%s\n", Input);
    
        return 0;
    }
    At the end it gave me these:
    Code:
    C:\HChingWong\MineSweeper>gcc -o main.exe main.c GetLine.c
    GetLine.c: In function 'GetLine':
    GetLine.c:2:14: error: 'NULL' undeclared (first use in this function)
    GetLine.c:2:14: note: each undeclared identifier is reported only once for each
    function it appears in
    GetLine.c:9:22: warning: incompatible implicit declaration of built-in function
    'strchr'
    How can I solve these disgusting things?

    And the other things I think are less useful.

    The GetLine.h
    Code:
    int GetLine(char *, int);
    And The GetLine.c
    Code:
    int GetLine(char *Input, const int SizeTag){
    	if(Input == NULL){
    		puts("Unallocated input buffer.");
    		return -1;
    	}
    
    	fgets(Input, SizeTag);
    
    	char *LastCharPtr = strchr(Input, '\n');
    	if(!LastCharPtr != NULL){
    		puts("Input is too long.\n");
                    *Input = '\0';
    		return -1;
    	}
    	*LastCharPtr = '\0';
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    In your GetLine.c, you need to include header files for all the functions/macros that you are using.
    ie. fgets, NULL, => stdio.h
    strchr => string.h

    Edit: fgets() expects three parameters.

    Btw, malloc.h is non-standard. Use stdlib.h.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Yeah~

    Thank you very much. I get it. I can only include files in the source files and not the headers. Thank you for the extra commands, too.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Erm, yes you can include headers in header files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. How to Link Various Files
    By Chinfrim in forum C Programming
    Replies: 29
    Last Post: 01-03-2008, 04:27 AM
  5. reading from files
    By recluse in forum C Programming
    Replies: 25
    Last Post: 12-26-2004, 10:33 AM