Weird Error in MinGW (Dev-C++)

J

JeffCameron

Hi everyone. I am using the latest MinGW that comes with Dev-C++ for
Windows. I am getting an impossible linker error when I try to call
some functions from a library that I wrote. I am avsolutely sure that
the library is linked. I would greatly appreciate a reply from someone
who knows the solution to this problem.

Here is a simplified version of my problem. In order to reproduce the
linking error that I am talking about, use the following steps:
- Use Dev-C++ to create a static library using hello.c and hello.h
- Create a new console application project with main.cpp as its
main source file
- Go to Project -> Project Options -> Parameters and click the Add
Library or Object button
- Add the library that was created in step 1
- Attempt to compile main.cpp

The error that I get is:
[Linker error] undefined reference to 'helloWorld()'

Here are the source files:
### hello.c ###
#include "hello.h"

void helloWorld()
{
printf("Hello World!\n");
}

### hello.h ###
#ifndef HELLO_H
#define HELLO_H

#include <stdio.h>

void helloWorld();

#endif

### main.cpp ###
#include "hello.h"

int main()
{
helloWorld();
return 0;
}

I would greatly appreciate a reply from someone who knows the solution
to this problem.

Jeff Cameron
 
S

Shawn McGrath

You are not linking helloWorld.c.

1. This is a C question, not a C++ question.
2. Your linker settings are not C or C++, they are Dev-C++, and by
extension MinGW and by further extension gcc problems.

Please ask in the appropriate places.

-Shawn.
 
J

JeffCameron

I'm afraid you didn't understand my question, Shawn. Linking hello.c
would obviously fix the problem. However, I have compiled hello.c
separately and turned it into a library. I want to link this library
to the final aplication. Can anyone solve my problem?
 
J

Jim Langston

JeffCameron said:
I'm afraid you didn't understand my question, Shawn. Linking hello.c
would obviously fix the problem. However, I have compiled hello.c
separately and turned it into a library. I want to link this library
to the final aplication. Can anyone solve my problem?

Actually, his reply is to this exact problem. You do not have a C or a C++
problem but a linker problem, "I want to link this library to the final
application."

Quote:
"2. Your linker settings are not C or C++, they are Dev-C++, and by
extension MinGW and by further extension gcc problems."

Ask in a gcc newsgroup, and you'll get an answer.

comp.lang.c++ is for c++ language question/answer/discussions as defined by
the standard, not specific compilers/linkers.
 
Y

Yuu

It seems that some people LOVE to reply with: "your question should be
asked somewhere else"... :-(

I think you simply need:

extern "C"
{
void helloWorld();
}

Then your header will work in C++ but not in C anymore.
That's why one can find the following idiom in several C/C++ libraries:

#ifdef __cplusplus
extern "C"
{
#endif

void helloWorld();

#ifdef __cplusplus
}
#endif


Sorry if i mistyped something (i'm writing this idiom from memory), but
basically this is it.
 
J

JeffCameron

Thanks Yuu. I'll try that.

You're right. I'm annoyed every time I see someone get rudely
dismissed with the age-old "this question doesn't belong here" reply.
There's just no point to that kind of behavior. Why not just answer
the question? And if you don't know the answer, then don't post. I'm
glad someone came along who actually knows the answer to my question.

Jeff Cameron
 
L

Lionel B

Thanks Yuu. I'll try that.

You're right. I'm annoyed every time I see someone get rudely
dismissed with the age-old "this question doesn't belong here" reply.
There's just no point to that kind of behavior. Why not just answer
the question? And if you don't know the answer, then don't post. I'm
glad someone came along who actually knows the answer to my question.

My dog has been a little off-colour lately and I suspect he may have
worms. What specific symptoms should I look for? (Please don't reply if
you don't know the answer).

;)
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Lionel said:
My dog has been a little off-colour lately and I suspect he may have
worms. What specific symptoms should I look for? (Please don't reply if
you don't know the answer).

42
 
Y

Yuu

Lionel B skribis:
My dog has been a little off-colour lately and I suspect he may have
worms. What specific symptoms should I look for? (Please don't reply if
you don't know the answer).

;)

But the OP's question wasn't off-topic after all (if my answer did
work). The fact that he put "MinGW" in the title doesn't make it
off-topic. Neither does the fact that he got a linker error (is there a
comp.lang.c++.linkererrors? :p). It's still C++, the only thing he
needed was an extern "C"...
 
O

Old Wolf

Shawn said:
You are not linking helloWorld.c.

Of course, since C files are not linked. However, he was linking
helloWorld.o (or .obj).
1. This is a C question, not a C++ question.

His file was called "main.cpp", which suggests that it is a C++
question.
2. Your linker settings are not C or C++, they are Dev-C++, and by
extension MinGW and by further extension gcc problems.

There is no problem with his linker settings.
Please ask in the appropriate places.

This is the appropriate place. Please get a clue
 
L

Lionel B

Lionel B skribis:

But the OP's question wasn't off-topic after all

It may or may not have been... not the point. My facetious reply was in
response to his little rant quoted above.

This is a high-volume newsgroup. If it becomes inundated with OT postings,
that serves no-one well. In this case it was not at all clear (to me
and evidently to others too) from the original posting whether the query
was in fact on or off topic; a cursory reading by the OP of the "how to
post" section in the FAQ would quite likely have avoided this situation.
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top