first time compiling with external .c and .h file, doesn't work

This is a discussion on first time compiling with external .c and .h file, doesn't work within the C Programming forums, part of the General Programming Boards category; Im trying to compile this program with gcc, i used this makefile: start: HW4.c header.h functions.c but the compiler isn't ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Angry first time compiling with external .c and .h file, doesn't work

    Im trying to compile this program with gcc, i used this makefile:
    start: HW4.c header.h functions.c

    but the compiler isn't finding the functions in the functions.c
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I don't know why this site renamed my filenames to fu.c and header..h -- they are functions.c and header.h on my computer; it compiled fine for me in visualC++ but doesn't work with gcc.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know why you attached them instead of just posting them. There aren't many people who prefer downloading files over reading a post.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, first of all you only need to include a header (.h) file once at the top of each C file where it's used. Repeatedly including it does nothing for you and may cause "redefinition" errors.

    Sedondly your header file needs "Include Guards"...
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    #include "stdio.h"
    #include "stdlib.h"
    #include <math.h>
    #include <time.h>
    
    extern char *create(int length);
    extern char *concat(char *string1, char *string2);
    extern int length(char *string);
    extern char *substring(char *string, int start, int length);
    extern char *copy(char *string);
    extern int compare(char *string1, char *string2);
    
    #endif  // HEADER_H
    These prevent the header from including itself into your code more than once per C file.


    It's not bad for a first attempt... I think you've got the general idea...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    #include "stdio.h"
    #include "stdlib.h"
    #include <math.h>
    #include <time.h>
    I'm not sure why you aren't just putting the standard includes in the files they are individually needed in, instead of putting them in this header. Also, I'm not sure why you are using "" instead of <> for those two includes.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I added the guards and took out the extra header includes, but it still isn't finding the functions in function.c -- something is wrong with my makefile or something. I got it to compile fine in Visual C++, but now when I try in GCC I get nothing but errors.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You do realize that you need to compile both C files, then link them into the executable... compiling only the main file will not work.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I assume you're typing "make start". You appear to have the right prerequisites -- what is the command line you have on the line below the one you give? (Or are you trying to use the automatic figure-it-out-from-the-file-extensions rules?)

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by CommonTater View Post
    You do realize that you need to compile both C files, then link them into the executable... compiling only the main file will not work.
    No, I really have no idea what I'm doing, and I don't know how to do that either. I have vi, and make, and gcc, and some kind of linux interface and I have no idea how I'm supposed to compile a program in it, nor can I find any coherent explanations. Visual C++ is so much easier, all I have to do is click "build" :<

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Adam Rinkleff View Post
    No, I really have no idea what I'm doing, and I don't know how to do that either. I have vi, and make, and gcc, and some kind of linux interface and I have no idea how I'm supposed to compile a program in it, nor can I find any coherent explanations. Visual C++ is so much easier, all I have to do is click "build" :<
    So it's your first time using a computer! Congratulations!

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by tabstop View Post
    I assume you're typing "make start". You appear to have the right prerequisites -- what is the command line you have on the line below the one you give? (Or are you trying to use the automatic figure-it-out-from-the-file-extensions rules?)
    If I type "make start", it says there is nothing to do with start. I don't have any other lines in my makefile, I think I'm trying to use the automatic rules which I was told would work if I did it right.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Okay, Please post, in full, and in separate sets of code tags, the following files:
    HW4.c
    functions.c
    header.h
    Makefile

    I'm guessing you don't know how make files work. The only line you posted for your make file says the start target relies on those files, but does not specify any action to perform, like "gcc -Wall -Werror $?". You probably need to read up a bit on make files. I strongly suggest using the -Wall (enable all warnings) and -Werror (treat warnings like errors, i.e. stop compilation on warnings) flags to ensure you're writing good, clean code.

    There's a huge number of other problems with your code that will become apparent when you try to compile (VC++ should have warned about much of this too, especially if you're compiling with /W3 or /W4). We can go over those once you post up your files.

    Our makefile tutorial
    http://www.gnu.org/software/make/man...Implicit-Rules

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include <math.h>
    #include <time.h>
    #include "header.h"
    
    void main()
    {
    	int x=0; int y=0;
    	int x2=0; int y2=0;
    
    
    	printf ("How long is string1? ");
    	scanf ("%d", &x);
    	printf ("How long is string2? ");
    	scanf ("%d", &y);
    
    	
    
    	printf("Input string1: "); char *ptr1=create(x);
    	printf("Input string2: "); char *ptr2=create(y);
    	printf("\nString1: %s",ptr1);
    	printf("\nString2: %s",ptr2);
    
    printf("\n\nLength of String1: %d", length(ptr1));
    printf("\nLength of String2: %d", length(ptr2));
    compare(ptr1, ptr2);
    
    printf("\nString3 = (1+2): %s", concat(ptr1, ptr2));
    char *ptr3=concat(ptr1, ptr2);
    printf("\nLength of String3: %d", length(ptr3));
    char *ptr5=copy(ptr3);
    printf("\n\nCopy of String3 (String5): %s\n\n", ptr5);
    
    int continu=1; fflush(stdin);
    
    	printf ("\n\nBegin substring at string3[x]: ");
    	scanf ("%d", &x2);
    	if (x2<0 || x2>(length(ptr3)-1)) {printf("\nInvalid start point"); continu=0;}
    
    
    
    	printf ("\nEnd substring at string3[y]: ");
    	scanf ("%d", &y2);
    	if (y2<x2 || y2>(length(ptr3))) {printf("\nInvalid end point"); continu=0;}
    
    	if (continu==1){
    		int sublength=y2-x2+1;
    		char *ptr4=substring(ptr3, x2, sublength);
    		printf("\n\nString4: %s\n\n", ptr4);
    	}
    
    	
    
    
    return 0;
    
    }

    functions.c
    Code:
    
    char *substring(char *string, int start, int length)
    {
    char *substring=(char *)malloc(length+1);
    
    int x=0;
    
    for (int i=start; i<=start+length-1; i++)
    {substring[x]=string[i]; x++;}
    
    substring[x]=0;
    
    
    
    
    return substring;
    
    
    
    
    
    }
    
    
    
    
    int length (char *string)
    {int x=0;
    
    for (x=0; *(string+x)!=0; x++){}
    
    return (x);
    }
    
    
    
    char *create(int length)
    {
    	char c=0, i=0;
    	char *input=(char *)malloc(length);
    	c=0;
    	input[0]=0;
    	fflush(stdin);
    
    	for (i=0; i<=length-1; i++)
    	{if (i<=length-1){c=getchar();	input[i]=c;}}
    	input[length]=0;
    
    	c=0;
    
    
    	return input;
    }
    
    
    
    
    char *copy(char *string){
    
    	int x=length(string);
    	char *copied=(char *)malloc(x+1);
    
    	for (int i=0; i<=x; i++)
    	{copied[i]=string[i];}
    
    	return copied;
    }
    
    
    
    
    
    char *concat(char *string1, char *string2)
    {
    int length1=length(string1);
    int length2=length(string2);
    int newlength=length(string1)+length(string2)+1;
    
    char *combined=(char *)malloc(newlength);
    
    for (int i=0; i<=length1-1; i++){*(combined+i)=*(string1+i);}
    
    for (int i=length1; i<=newlength; i++){*(combined+i)=*(string2+i-length1);}
    
    
    return combined;
    }
    
    
    
    int compare(char *string1, char *string2){
    
    	int x=strcmp(string1, string2); 
    	if (x==0){printf("\n\nThe two strings are equal.\n\n");}
    	if (x<0){printf("\n\nString1 is less than String2.\n\n");}
    	if (x>0){printf("\n\nString1 is greater than String2.\n\n");}
    
    
    return 0;
    }
    header.h
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    #include "stdio.h"
    #include "stdlib.h"
    #include <math.h>
    #include <time.h>
    
    extern char *create(int length);
    extern char *concat(char *string1, char *string2);
    extern int length(char *string);
    extern char *substring(char *string, int start, int length);
    extern char *copy(char *string);
    extern int compare(char *string1, char *string2);
    
    #endif  // HEADER_H
    makefile
    Code:
    start: HW4.c header.h functions.c

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by anduril462 View Post
    The only line you posted for your make file says the start target relies on those files, but does not specify any action to perform, like "gcc -Wall -Werror $?".
    I was told make would know what to do. I certainly don't know what to do. I just want it to compile them the most simple basic way it can.

    Quote Originally Posted by anduril462 View Post
    There's a huge number of other problems with your code that will become apparent when you try to compile
    Well, the original of this worked fine with VisualC++. I'm sure there are some differences that will have to be ironed out, which is why I was trying to do this in order to see what those differences are, but I can't even get it to compile. I did get it to compile as one big file, but I'm trying to figure out how to compile with more than one file.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Code:
    x: HW4.o functions.o
    gcc HW4.o functions.o -o x
    
    HW4.o: HW4.c header.h
    functions.o: functions.c header.h
    I also tried this, but I get a separator error whatever that is.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .exe file doesn't work
    By sara12345 in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 12:18 PM
  2. Replies: 2
    Last Post: 05-13-2009, 04:25 PM
  3. binary file i/o -- why doesn't this work?
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2009, 09:39 AM
  4. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  5. Time Delay doesn't work!
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-17-2001, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21