Question about compiling files

B

bowlderyu

Hello, all.
I meet a question about compiling files.
Eg., I have three files named myfile.h, myfile.c and the main.c file,
and there is a function, ex, void myfun(...).

If I put myfun(...) in the main.c file like the following,

.....
void myfun(...)
{
.....
}
int main(void)
{
....
}

it works well.

But when I move myfun(...) to the file of myfile.c, and a make error
message appears when the same command "make" runs, although I have
declear the myfun(...) function in the main function.

The message seems that the myfun(...) is not decleared before used.

And this is my Makefile:
#test Makefile
objects=main.o
test:$(objects)
cc -o test $(objects) -lgsl -lgslcblas -lm
..PHONY:clean
clean:
rm test $(objects)

My code may be not very clear, so I don't attach it here.

I just move a function from one file to another, and give some
declearation in the header and main files. But it cann't work.

It will be appreciate for you help.

Thanks.
 
J

James Kuyper

bowlderyu said:
Hello, all.
I meet a question about compiling files.
Eg., I have three files named myfile.h, myfile.c and the main.c file,
and there is a function, ex, void myfun(...).

If I put myfun(...) in the main.c file like the following,

....
void myfun(...)
{
....
}
int main(void)
{
...
}

it works well.

But when I move myfun(...) to the file of myfile.c, and a make error
message appears when the same command "make" runs, although I have
declear the myfun(...) function in the main function.

The message seems that the myfun(...) is not decleared before used.

And this is my Makefile:
#test Makefile
objects=main.o
test:$(objects)
cc -o test $(objects) -lgsl -lgslcblas -lm
.PHONY:clean
clean:
rm test $(objects)

My code may be not very clear, so I don't attach it here.

That decision makes it pretty nearly impossible to help you. Your code
apparently has a mistake, but without seeing the code, we can't make any
guesses about what the mistake is. Well, that's not quite true - it
seems virtually certain that there's a problem with your declaration of
the function in main.c; but without seeing your code, we have no idea
what that problem is.

Simplify the code as much as possible, while still showing the problem.
The show us the full text of your simplified program. Don't remove
anything from the final program when you show it to us. You have a
problem that you don't understand - therefore, it's a virtual certainty
that whatever the problem with your code actually is, it will be one of
the things you decide to cut out, because you incorrectly think that it
isn't relevant.

At a minimum, your code needs the definition of main(), including the
declaration of myfun() and the call to myfun(). Also, please provide the
full text of the error messages produced when you tried to compile it.
 
B

Barry Schwarz

Hello, all.
I meet a question about compiling files.
Eg., I have three files named myfile.h, myfile.c and the main.c file,
and there is a function, ex, void myfun(...).

If I put myfun(...) in the main.c file like the following,

....
void myfun(...)
{
....
}
int main(void)
{
...
}

it works well.

But when I move myfun(...) to the file of myfile.c, and a make error
message appears when the same command "make" runs, although I have
declear the myfun(...) function in the main function.

If you move the definition of myfun to a different c file, HOW do you
declare it before it is used?
The message seems that the myfun(...) is not decleared before used.

Messages don't seem anything. Or what they seem to you may not be
what they seem to someone else. Show us the exact text of the error
message, preferably by cut and paste, not retyping. Is the message
generated in the compile phase or the link phase? If during the
compile phase, you are also going to have to show us the relevant
parts of the code (at a minimum, the #include directives, all function
definition signatures, most declarations, and several lines leading up
to the line in question).
And this is my Makefile:
#test Makefile
objects=main.o
test:$(objects)
cc -o test $(objects) -lgsl -lgslcblas -lm
.PHONY:clean
clean:
rm test $(objects)

Make files are off-topic here because they are not part of the
language and differ from system to system. You would get better
advice in a group that discusses your system.

However, when you move the function to a different .c file, you now
have two .c files to compile. That means you should have two .o files
to link. How does your make file know about the second one?
My code may be not very clear, so I don't attach it here.

I just move a function from one file to another, and give some
declearation in the header and main files. But it cann't work.

Why do you duplicate declarations in a .h and a .c file?
 
B

bowlderyu

Richard Heathfield said:
bowlderyu said:


/* myfile.h */
#ifndef H_MYFILE_H
#define H_MYFILE_H 1
void myfun(void);
#endif

/* myfile.c */
#include "myfile.h"
#include <stdio.h>
void myfun(void)
{
puts("Hello, world!");
}

/* main.c */
#include "myfile.h"
int main(void)
{
myfun();
return 0;
}
Thank you for your suggestion.
I reedit the files as above, but the same message apears.
And I comment where myfun() is used in the main.c file, it can be
compiled.

It is surprised to me when I remove the comment above, it works well!

I mean, I just add comment, compile; remove comment, compile. It can
work.

I don't know the reason. Maybe some files need to update?

Anyway, thanks all of you.
It's well worth writing clear code, if only so that it makes it easier for
others to help you.

Good suggestion.
 
L

lovecreatesbeauty

With GNU tools, the simplest way to write a Makefile to build my example
would be as follows (with [TAB] representing a hard tab character):

WARN = -W -Wall -ansi -pedantic
CC = gcc
myprog: main.o myfile.o
[TAB]$(CC) $(WARN) -o myprog main.o myfile.o
main.o: main.c
[TAB]$(CC) $(WARN) -c -o main.o main.c
myfile.o: myfile.c
[TAB]$(CC) $(WARN) -c -o myfile.o myfile.c

I think this is simpler Makefile

CC = gcc
CFLAGS = -ansi -pedantic -Wall -W
LDFLAGS =
OBJS = a.o b.o
OUT = a.out

$(OUT) : $(OBJS)
$(CC) $(LDFLAGS) $^ -o $@

a.o b.o : b.h

..PHONY : clean
clean :
rm $(OUT) $(OBJS)
 
B

bowlderyu

Richard Heathfield said:
bowlderyu said:
Thank you for your suggestion.
I reedit the files as above, but the same message apears.

Then it's probably because your Makefile is screwed.

With GNU tools, the simplest way to write a Makefile to build my example
would be as follows (with [TAB] representing a hard tab character):

WARN = -W -Wall -ansi -pedantic
CC = gcc
myprog: main.o myfile.o
[TAB]$(CC) $(WARN) -o myprog main.o myfile.o
main.o: main.c
[TAB]$(CC) $(WARN) -c -o main.o main.c
myfile.o: myfile.c
[TAB]$(CC) $(WARN) -c -o myfile.o myfile.c

Now, the above code is a little hard for me to understand in detail.

I need work hard for it.

Regards,

bowlderyu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top