compiler shouts an error with the make file
After a certain attempt to write a simple program with a main and one function,
I ask for your help to find the bug. I include the 3 files that are in action:
- the main function in
base.c
- the function in
fun.c
- the
makefile
The compiler says that the function is called in a bad way in the main:
undefined reference to `fun'
base.c
#include <stdio.h>
int fun(char c);
main()
{
printf("please enter a single charn");
char c=getchar();
fun(c);
return 0;
}
fun.c
#include <stdio.h>
int fun(char c)
{
printf("%d3 is the value of your char!n", 'c');
return 0;
}
makefile
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -g -Wall -ansi fun.c -o fun.o
c makefile
add a comment |
After a certain attempt to write a simple program with a main and one function,
I ask for your help to find the bug. I include the 3 files that are in action:
- the main function in
base.c
- the function in
fun.c
- the
makefile
The compiler says that the function is called in a bad way in the main:
undefined reference to `fun'
base.c
#include <stdio.h>
int fun(char c);
main()
{
printf("please enter a single charn");
char c=getchar();
fun(c);
return 0;
}
fun.c
#include <stdio.h>
int fun(char c)
{
printf("%d3 is the value of your char!n", 'c');
return 0;
}
makefile
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -g -Wall -ansi fun.c -o fun.o
c makefile
In the code forfun()
, your argument toprintf()
should be justc
and not'c'
. You probably also mean%3d
rather than%d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicitint main(void) { … }
.
– Jonathan Leffler
Nov 24 '18 at 17:45
1
The compilation problem is that you forgot the-c
flags in the compiler line forbase.o
andfun.o
. Interestingly, the simplest fix would be to delete the two compiler command for the two object files —make
knows how to compile C files to object files. You could setCFLAGS += -Wall
(orCFLAGS = -Wall
) to get the (very important)-Wall
flag included. Adding-Werror
too would be good.
– Jonathan Leffler
Nov 24 '18 at 17:49
thank you very much! it works now
– davidku
Nov 24 '18 at 18:06
1
Sidenote: ANSI-C is outdated since almost 20 years. Use modern standard C, i.e. C11 resp. 17.
– too honest for this site
Nov 24 '18 at 18:45
add a comment |
After a certain attempt to write a simple program with a main and one function,
I ask for your help to find the bug. I include the 3 files that are in action:
- the main function in
base.c
- the function in
fun.c
- the
makefile
The compiler says that the function is called in a bad way in the main:
undefined reference to `fun'
base.c
#include <stdio.h>
int fun(char c);
main()
{
printf("please enter a single charn");
char c=getchar();
fun(c);
return 0;
}
fun.c
#include <stdio.h>
int fun(char c)
{
printf("%d3 is the value of your char!n", 'c');
return 0;
}
makefile
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -g -Wall -ansi fun.c -o fun.o
c makefile
After a certain attempt to write a simple program with a main and one function,
I ask for your help to find the bug. I include the 3 files that are in action:
- the main function in
base.c
- the function in
fun.c
- the
makefile
The compiler says that the function is called in a bad way in the main:
undefined reference to `fun'
base.c
#include <stdio.h>
int fun(char c);
main()
{
printf("please enter a single charn");
char c=getchar();
fun(c);
return 0;
}
fun.c
#include <stdio.h>
int fun(char c)
{
printf("%d3 is the value of your char!n", 'c');
return 0;
}
makefile
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -g -Wall -ansi fun.c -o fun.o
c makefile
c makefile
edited Nov 24 '18 at 17:49
Jonathan Leffler
569k916821032
569k916821032
asked Nov 24 '18 at 17:30
davidkudavidku
94
94
In the code forfun()
, your argument toprintf()
should be justc
and not'c'
. You probably also mean%3d
rather than%d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicitint main(void) { … }
.
– Jonathan Leffler
Nov 24 '18 at 17:45
1
The compilation problem is that you forgot the-c
flags in the compiler line forbase.o
andfun.o
. Interestingly, the simplest fix would be to delete the two compiler command for the two object files —make
knows how to compile C files to object files. You could setCFLAGS += -Wall
(orCFLAGS = -Wall
) to get the (very important)-Wall
flag included. Adding-Werror
too would be good.
– Jonathan Leffler
Nov 24 '18 at 17:49
thank you very much! it works now
– davidku
Nov 24 '18 at 18:06
1
Sidenote: ANSI-C is outdated since almost 20 years. Use modern standard C, i.e. C11 resp. 17.
– too honest for this site
Nov 24 '18 at 18:45
add a comment |
In the code forfun()
, your argument toprintf()
should be justc
and not'c'
. You probably also mean%3d
rather than%d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicitint main(void) { … }
.
– Jonathan Leffler
Nov 24 '18 at 17:45
1
The compilation problem is that you forgot the-c
flags in the compiler line forbase.o
andfun.o
. Interestingly, the simplest fix would be to delete the two compiler command for the two object files —make
knows how to compile C files to object files. You could setCFLAGS += -Wall
(orCFLAGS = -Wall
) to get the (very important)-Wall
flag included. Adding-Werror
too would be good.
– Jonathan Leffler
Nov 24 '18 at 17:49
thank you very much! it works now
– davidku
Nov 24 '18 at 18:06
1
Sidenote: ANSI-C is outdated since almost 20 years. Use modern standard C, i.e. C11 resp. 17.
– too honest for this site
Nov 24 '18 at 18:45
In the code for
fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
.– Jonathan Leffler
Nov 24 '18 at 17:45
In the code for
fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
.– Jonathan Leffler
Nov 24 '18 at 17:45
1
1
The compilation problem is that you forgot the
-c
flags in the compiler line for base.o
and fun.o
. Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make
knows how to compile C files to object files. You could set CFLAGS += -Wall
(or CFLAGS = -Wall
) to get the (very important) -Wall
flag included. Adding -Werror
too would be good.– Jonathan Leffler
Nov 24 '18 at 17:49
The compilation problem is that you forgot the
-c
flags in the compiler line for base.o
and fun.o
. Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make
knows how to compile C files to object files. You could set CFLAGS += -Wall
(or CFLAGS = -Wall
) to get the (very important) -Wall
flag included. Adding -Werror
too would be good.– Jonathan Leffler
Nov 24 '18 at 17:49
thank you very much! it works now
– davidku
Nov 24 '18 at 18:06
thank you very much! it works now
– davidku
Nov 24 '18 at 18:06
1
1
Sidenote: ANSI-C is outdated since almost 20 years. Use modern standard C, i.e. C11 resp. 17.
– too honest for this site
Nov 24 '18 at 18:45
Sidenote: ANSI-C is outdated since almost 20 years. Use modern standard C, i.e. C11 resp. 17.
– too honest for this site
Nov 24 '18 at 18:45
add a comment |
1 Answer
1
active
oldest
votes
The compilation problem is that you forgot the -c
flags in the compiler line for base.o
and fun.o
. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror
too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c
fun.o: fun.c
In the code for fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
. And you should probably create a header fun.h
containing:
extern int fun(char c);
and #include "fun.h"
in both source files, and add fun.h
after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make
will infer that dependency automatically. But you do need to specify the header file dependency.
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
make
comes equipped with a lot of predefined macros, such asCFLAGS
andCC
. You can see which ones are built in withmake -p -f /dev/null
, for example.
– Jonathan Leffler
Nov 24 '18 at 19:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460701%2fcompiler-shouts-an-error-with-the-make-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The compilation problem is that you forgot the -c
flags in the compiler line for base.o
and fun.o
. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror
too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c
fun.o: fun.c
In the code for fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
. And you should probably create a header fun.h
containing:
extern int fun(char c);
and #include "fun.h"
in both source files, and add fun.h
after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make
will infer that dependency automatically. But you do need to specify the header file dependency.
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
make
comes equipped with a lot of predefined macros, such asCFLAGS
andCC
. You can see which ones are built in withmake -p -f /dev/null
, for example.
– Jonathan Leffler
Nov 24 '18 at 19:25
add a comment |
The compilation problem is that you forgot the -c
flags in the compiler line for base.o
and fun.o
. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror
too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c
fun.o: fun.c
In the code for fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
. And you should probably create a header fun.h
containing:
extern int fun(char c);
and #include "fun.h"
in both source files, and add fun.h
after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make
will infer that dependency automatically. But you do need to specify the header file dependency.
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
make
comes equipped with a lot of predefined macros, such asCFLAGS
andCC
. You can see which ones are built in withmake -p -f /dev/null
, for example.
– Jonathan Leffler
Nov 24 '18 at 19:25
add a comment |
The compilation problem is that you forgot the -c
flags in the compiler line for base.o
and fun.o
. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror
too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c
fun.o: fun.c
In the code for fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
. And you should probably create a header fun.h
containing:
extern int fun(char c);
and #include "fun.h"
in both source files, and add fun.h
after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make
will infer that dependency automatically. But you do need to specify the header file dependency.
The compilation problem is that you forgot the -c
flags in the compiler line for base.o
and fun.o
. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror
too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c
fun.o: fun.c
In the code for fun()
, your argument to printf()
should be just c
and not 'c'
. You probably also mean %3d
rather than %d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }
. And you should probably create a header fun.h
containing:
extern int fun(char c);
and #include "fun.h"
in both source files, and add fun.h
after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $@
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make
will infer that dependency automatically. But you do need to specify the header file dependency.
answered Nov 24 '18 at 19:07
Jonathan LefflerJonathan Leffler
569k916821032
569k916821032
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
make
comes equipped with a lot of predefined macros, such asCFLAGS
andCC
. You can see which ones are built in withmake -p -f /dev/null
, for example.
– Jonathan Leffler
Nov 24 '18 at 19:25
add a comment |
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
make
comes equipped with a lot of predefined macros, such asCFLAGS
andCC
. You can see which ones are built in withmake -p -f /dev/null
, for example.
– Jonathan Leffler
Nov 24 '18 at 19:25
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
isn't ${CC} should be defined in Makefile?
– Sajjad Ahmed
Nov 24 '18 at 19:24
make
comes equipped with a lot of predefined macros, such as CFLAGS
and CC
. You can see which ones are built in with make -p -f /dev/null
, for example.– Jonathan Leffler
Nov 24 '18 at 19:25
make
comes equipped with a lot of predefined macros, such as CFLAGS
and CC
. You can see which ones are built in with make -p -f /dev/null
, for example.– Jonathan Leffler
Nov 24 '18 at 19:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460701%2fcompiler-shouts-an-error-with-the-make-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In the code for
fun()
, your argument toprintf()
should be justc
and not'c'
. You probably also mean%3d
rather than%d3
though that 'works'; it just doesn't do what you expect. Note that you should use an explicitint main(void) { … }
.– Jonathan Leffler
Nov 24 '18 at 17:45
1
The compilation problem is that you forgot the
-c
flags in the compiler line forbase.o
andfun.o
. Interestingly, the simplest fix would be to delete the two compiler command for the two object files —make
knows how to compile C files to object files. You could setCFLAGS += -Wall
(orCFLAGS = -Wall
) to get the (very important)-Wall
flag included. Adding-Werror
too would be good.– Jonathan Leffler
Nov 24 '18 at 17:49
thank you very much! it works now
– davidku
Nov 24 '18 at 18:06
1
Sidenote: ANSI-C is outdated since almost 20 years. Use modern standard C, i.e. C11 resp. 17.
– too honest for this site
Nov 24 '18 at 18:45