imre
imre

Reputation: 489

Linking multiple .c files

I have a C file named first.c in which I define an array and call a function which is defined in a C file named second.c. This is how first.c looks:

int main(void)
{
    int array[100];
    myFunc(*array);
}

second.c on the other hand looks like this:

void myFunc(int array)
{
    ...
}

But anytime I try to compile these, second.c gives me errors as if it had no idea about the array I passed to its function as an argument. I guess the function doesn't know that at the linking stage. I compile these like this:

gcc -O2 -std=c99 -Wall -pedantic -lm second.c -c
gcc -O2 -std=c99 -Wall -pedantic first.c -c
gcc second.o first.o -o finished

But that's just what I came up with and of course it doesn't work. I guess a Makefile would be in place, but I'm not sure how to implement it.

Upvotes: 0

Views: 73

Answers (1)

Blue Ice
Blue Ice

Reputation: 7930

Your issue may lie in that the received value is not a pointer- so change void myFunc(int array) to void myFunc(int* array).

Upvotes: 4

Related Questions