I know I've made multiple posts on this subject, but I can't seem to figure out how to compile multiple source files using header files. I read the FAQ and previous posts and I'm doing everything right I think, here's my code.
MAIN CODE
Code:
#include "myString.h"
int wordCount (const char text[]);
void readLine(char buffer[]);
int main(void) {
bool endOfText = false;
char text[81];
int count = 0;
while (!endOfText) {
readLine(text);
if (text[0] == '\0')
endOfText = true;
else
count += wordCount(text);
}
printf("Number of words equals %i", count);
return 0;
}
FUNCTION DECLARATION CODE
Code:
#include "myString.h"
bool isNumericChar (const char c) {
if ((c >= '0' && c <= '9') || c == ',' || c == '.' || c == '-')
return true;
else
return false;
}
bool isAlphaChar (const char c) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '\'')
return true;
else
return false;
}
void readLine (char buffer[]) {
char letter;
int i = 0;
do {
letter = getchar();
buffer[i] = letter;
i++;
} while (letter != '\n');
buffer[i - 1] = '\0';
}
int wordCount (const char text[]) {
bool lookForWord = true;
int count = 0, i;
for (i = 0; text[i] != '\0'; i++) {
if (isAlphaChar(text[i]) || isNumericChar(text[i])) {
if (lookForWord) {
count++;
lookForWord = false;
}
}
else
lookForWord = true;
}
return count;
}
bool isEqual (const char s1[], const char s2[]) {
int i = 0;
while (s1[i] == s2[i] && s1[i] != '\0')
i++;
if (s1[i] == '\0' && s2[i] == '\0')
return true;
else
return false;
}
void concat (char result[], const char s1[], const char s2[]) {
int i = 0, j = 0;
for (i = 0; s1[i] != '\0'; i++)
result[i] = s1[i];
for (j = 0; s2[j] != '\0'; j++)
result[i + j] = s2[j];
result[i + j] = '\0';
}
int compareTo (const char s1[], const char s2[]) {
int i = 0;
while (s1[i] == s2[i] && s1[i] != '\0')
i++;
if (s1[i] < s2[i])
return -1;
else if (s1[i] == s2[i])
return 0;
else
return 1;
}
int strToInt (const char s[]) {
int intValue, result = 0, i;
for (i = 0; s[i] >= '0' && s[i] <= '9'; i++) {
intValue = s[i] - '0';
result = result * 10 + intValue;
}
return result;
}
HEADER FILE CODE
Code:
#include <stdio.h>
#include <stdbool.h>
#ifndef MYSTRING_H
#define MYSTRING_H
// functions
bool isNumericChar (const char c);
bool isAlphaChar (const char c);
int wordCount (const char text[]);
void readLine(char buffer[]);
bool isEqual (const char s1[], const char s2[]);
void concat (char result[], const char s1[], const char s2[]);
int compareTo (const char s1[], const char s2[]);
int strToInt (const char s[]);
#endif
I used jGRASP as my IDE and I put all these files into a project and I tried compiling the project, but it doesn't seem to compile the multiple source files. I tried to compile/link each individual file and I get the following error in main
Code:
text.c:1:
myString.h:17:8: warning: no newline at end of file
text.c:25:2: warning: no newline at end of file
/cygdrive/c/DOCUME~1/jshowa/LOCALS~1/Temp/ccAGX7Qk.o: In function `main':
text.c:14: undefined reference to `_readLine'
text.c:19: undefined reference to `_wordCount'
collect2: ld returned 1 exit status
I also get segmentation fault errors when I try to compile the header file, but that's probably because I'm trying to compile a preprocessor file which isn't really compiled.
I tried compiling it using the command line (gcc) and it worked, but I can't figure out why I can't use the IDE to do it.
I looked in the help file under projects and I found this
How To Compile a Program
For languages such as Java with run-time linking, "Compiler" / "Compile" will compile the file, and in the case of Java, out-of-date dependencies (note that dependencies are compiled, not dependents). For languages such as C++ with build-time linking, "Compiler" / "Compile and Link" will create the executable for a single-file program, while "Compile" will compile the file only.[Interesting part]--> jGRASP does not provide a "build" function for multiple file programs in languages with build-time linking. For such programs, you should write a makefile or other build script.<-- For most jGRASP compiler environments, "Compiler" / "Make" will call the "make" command in the directory containing the file.
So I tried making a make file and that made it work to, however I can't run the program in the IDE and it confuses me because I looked in two books on compiling and linking multiple source files, Programming in C and K&R and they tell me the exact same thing I'm doing! So I wonder how they did this back in the day before make existed.
Anyways, if anyone can help me understand this or has any advice or comments that can help me clear this up, it would be of great help. Thank you.
Oh and if there are any jGRASP users, please let me know if I'm doing this right.