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
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
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.
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.
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"...
These prevent the header from including itself into your code more than once per C file.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
It's not bad for a first attempt... I think you've got the general idea...
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.Code:#ifndef HEADER_H #define HEADER_H #include "stdio.h" #include "stdlib.h" #include <math.h> #include <time.h>
Quzah.
Hope is the first step on the road to disappointment.
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.
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.
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?)
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" :<
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
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
header.hCode: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; }
makefileCode:#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
Code:start: HW4.c header.h functions.c
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.
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.
I also tried this, but I get a separator error whatever that is.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