C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ noob - compiler question.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Sheppe
Guest





PostPosted: Tue Aug 24, 2004 10:51 pm    Post subject: C++ noob - compiler question. Reply with quote



I'm not new to programming, but I am fairly new to C++. I've got a bit of a
problem with a project that I am working on. My environment is as follows:

OS: Mandrake 10
IDE: Anjuta
Compiler: gnu

The project I am working on is a C/C++ combo. I've created a class (in C++)
to encapsulate a particular bit of functionality; this class makes calls to
some C functions in a separate file (nothing complicated). All of the
necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it compile but
not build?

Thanks.

-Sheppe


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Raoul Gough
Guest





PostPosted: Wed Aug 25, 2004 11:36 am    Post subject: Re: C++ noob - compiler question. Reply with quote



Sheppe <noone (AT) nowhere (DOT) com> writes:

[snip]
Quote:
The problem is this: when I "Compile" my file, it works fine and produces no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it compile but
not build?

Probably a linkage specification problem. C++ compilers generally use
name "mangling" to provide type-safe linkage, so the (C++) function
declaration

extern void function_name_here(int, int);

will actually produce a symbol for the linker like
__Z18function_name_hereii (this is what g++ 3.3.1 actually produces,
other compilers may use a different scheme). A plain old C function
with the same prototype will produce a non-mangled symbol like
_function_name_here. The linker won't be able to match these symbols
for the obvious reason.

Because of this, if you want to use a C function from within C++, you
have to notify the C++ compiler, by declaring the function like this:

extern "C" void function_name_here (int, int);

Typically, header files (probably including most everything in
/usr/include on your system) that are compatible with C and C++ do
this:

#ifdef __cplusplus
extern "C" { // Enclose all of the rest in this linkage spec
#endif

/* C and C++ compatible stuff */

#ifdef __cplusplus
}
#endif

Actually, the name mangling is just a common implementation
detail, and there may be other reasons why you need to tell the
compiler that it's a plain C function (e.g. calling conventions may
differ).

--
Raoul Gough.
export LESS='-X'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Frédéric Lachasse
Guest





PostPosted: Wed Aug 25, 2004 11:38 am    Post subject: Re: C++ noob - compiler question. Reply with quote



"Sheppe" <noone (AT) nowhere (DOT) com> wrote

Quote:
I'm not new to programming, but I am fairly new to C++. I've got a bit of
a
problem with a project that I am working on. My environment is as follows:

OS: Mandrake 10
IDE: Anjuta
Compiler: gnu

The project I am working on is a C/C++ combo. I've created a class (in
C++)
to encapsulate a particular bit of functionality; this class makes calls
to
some C functions in a separate file (nothing complicated). All of the
necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces
no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it compile
but
not build?

You need to put the C definitions in the C++ program inside an extern "C"
block, for example, if the definitions are in a header cdefs.h:

extern "C" {
#include "cdefs.h"
}

If you want to understand why, look for extern "C" and name mangling in C++
documentation.

--
Frédéric Lachasse - ECP86


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Mooney
Guest





PostPosted: Wed Aug 25, 2004 11:41 am    Post subject: Re: C++ noob - compiler question. Reply with quote

Sheppe wrote:
<snip>
Quote:
The problem is this: when I "Compile" my file, it works fine and
produces no errors, but when I go to "Build" my file I get errors in
the file that I just compiled with no errors. The errors read
"undefined reference to function_name_here(param1*, param2)". The
errors are produced on each line that makes a call to a function in
the C file.

I'm a little confused as to what I need to do here. Why does it
compile but not build?

The "undefined reference" message is from your linker. The compiler is
responsible for translating your source code into object modules. The
linker is responsible for knitting object modules (yours and others) into an
executable.

Somewhere (likely in a header (.h) file) you have _declared_ a function:
void function_name_here(...); Somewhere else you have invoked that function
through a call. The format of the call matches the prototype, so the
compiler is happy. It generates an external reference for the linker to
resolve. Unfortunately, you haven't _defined_ the function (you haven't
supplied that actual function with a body (typically in a .cpp file)). So
you've broken the promise you've made in the header file. Supply the
promised function and all should be well. (Well the linker should stop
complaining anyway. <g>)

--
TFM3

Note: Spam-resistant e-mail address





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David B. Held
Guest





PostPosted: Wed Aug 25, 2004 11:42 am    Post subject: Re: C++ noob - compiler question. Reply with quote

Sheppe wrote:

Quote:
[...]
The problem is this: when I "Compile" my file, it works fine and produces no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it compile but
not build?

Because compiling only checks the translation units in question. It
does not resolve external references. That's the job of the linker.
When you build the project, the linker gets invoked, and finds that
some functions that you refer to in your source files don't have a
definition anywhere in your project. You probably need to add the
correct object/source files to your project so that the linker can see
all the definitions that it is looking for. So you might have a file
called file1.cpp that refers to a function called foo() located in
file2.c. You either want to add file2.c to your project, or you want
to add file2.o to your project. Another alternative is if file2.o is
in a library called lib2.a. Then you want to add that library to your
project so the linker knows to search it.

Dave

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nathan
Guest





PostPosted: Wed Aug 25, 2004 11:43 am    Post subject: Re: C++ noob - compiler question. Reply with quote

Sheppe wrote:

Quote:
The problem is this: when I "Compile" my file, it works fine and
produces no
errors, but when I go to "Build" my file I get errors in the file
that I just compiled with no errors. The errors read "undefined
reference to
function_name_here(param1*, param2)". The errors are produced on each
line that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it
compile but not build?

From what I can determine is that you are not compiling and then
linking the file with the C functions. The easiest way to do this is

to create a Makefile, and then use the command "make" to compile your
project. Here are some sites I got from a quick search on Google for
some Makefile tutorials:

http://www.groovyweb.uklinux.net/index.php?page_name=how%20to%20write%20a%20makefile

http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/make.html

http://sources.redhat.com/autobook/autobook/autobook_toc.html

http://www.psc.edu/~welling/33-456/notes/GNUmakeTutorial.html

If this is not the problem you are having, are you protoyping your
functions? In the file with the Class, do you have a list of the
functions you plan to use?

i.e:



// File: myclass.cpp

void callCFunction(); // This is some function from your file
// containing the C functions.
[ ... ]

void someclass::dosomething()
{
[ ... ]
callCFunction();
}

Well, I hope this helps!

Nathan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Conrad S.
Guest





PostPosted: Wed Aug 25, 2004 11:49 am    Post subject: Re: C++ noob - compiler question. Reply with quote


"Sheppe" <noone (AT) nowhere (DOT) com> schreef in bericht
news:KyLWc.208944$gE.192331 (AT) pd7tw3no (DOT) ..
Quote:
I'm not new to programming, but I am fairly new to C++. I've got a bit of
a
problem with a project that I am working on. My environment is as follows:

OS: Mandrake 10
IDE: Anjuta
Compiler: gnu

The project I am working on is a C/C++ combo. I've created a class (in
C++)
to encapsulate a particular bit of functionality; this class makes calls
to
some C functions in a separate file (nothing complicated). All of the
necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces
no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it compile
but
not build?

Thanks.

-Sheppe


When you compile a file you translate _only_ one C or CPP file (including
any include files)
into a single object file.Calls to functions in other files (your C
functions) are not resolved
yet; the compiler doesn't know about their existance just accepts that they
do on good faith.

Now when you build your project, all these object files are linked together
to make an executable.
For this all references which the compiler accepted on good faith must now
be resolved. That is
if you called function ThisIsNifty() in file MyClass.cpp (which has been
compiled into MyClass.obj),
the linker will search all other object files to find which one contains
ThisIsNifty(). If the linker
fails in its quest it gives up and tells you it can not do what you want it
to do.
Looking up a name might seem straightforward but when you throw in C++ it is
not. To allow
function overloading C++ does a little trick called name mangling: the
compler actualy changes the
name of your function to something else. Thus when you think you are calling
"int ThisIsNifty( int )"
from your cpp file you end up calling @@int@ThisIsNifty@@int or something
similarly weird.

So what probably happened in your case is that the linker could not match up
the names in your
C++ part with the ones in your C part. As it is not uncommen what you are
trying to do, the
C++ language allows you to explicitely turn of name mangling by wrapping
part of the code
like this:

extern ("C")
{
}

Antoon



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Aug 25, 2004 8:50 pm    Post subject: Re: C++ noob - compiler question. Reply with quote

Sheppe wrote:
Quote:
The project I am working on is a C/C++ combo. I've created a class (in
C++) to encapsulate a particular bit of functionality; this class makes
calls to some C functions in a separate file (nothing complicated). All of
the necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces
no errors, but when I go to "Build" my file I get errors in the file that
I just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

Firstly, these errors are linker errors, not compiler errors.
'undefined reference' means that something you promised to provide is not
there. Another common one is 'multiple definition' which means there are
too many of some thing.

In your case, I guess it is related to the difference between a C function
prototype and a C++ function declaration. The line
void function( char*);
declares a function, but differently for both languagues. In order to access
C functions in c++, you have to either prefix them like this:
extern "C" void function( char*);
or, more convenient when dealing with a whole bunch of them like this:
extern "C" {
void function( char*);
void another_function( char*);
}

However, C doesn't know these extern statements, which is why you need to
hide them in order to share headers:
#ifdef __cplusplus
extern "C" {
#endif
void function( char*);
void another_function( char*);
#ifdef __cplusplus
}
#endif

HTH

Uli

--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Timo Geusch
Guest





PostPosted: Wed Aug 25, 2004 8:55 pm    Post subject: Re: C++ noob - compiler question. Reply with quote

Sheppe was seen penning the following ode to ... whatever:
Quote:
I'm not new to programming, but I am fairly new to C++. I've got a bit of a
problem with a project that I am working on. My environment is as follows:

OS: Mandrake 10
IDE: Anjuta
Compiler: gnu

The project I am working on is a C/C++ combo. I've created a class (in C++)
to encapsulate a particular bit of functionality; this class makes calls to
some C functions in a separate file (nothing complicated). All of the
necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm not familiar with that particular IDE - when you say 'build' as
opposed to compile, I assume that your IDE is attempting to link the
object files generated by the compile stage, correct?

Did you compile the 'C' file as well? Are you trying to link both into
the same executable?

If that's the case, did you #include the C header marked as 'extern
"C"' so the compiler knows to turn off the C++ name mangling for these
particular prototypes?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mike Capp
Guest





PostPosted: Wed Aug 25, 2004 9:43 pm    Post subject: Re: C++ noob - compiler question. Reply with quote

Sheppe <noone (AT) nowhere (DOT) com> wrote


Quote:
The project I am working on is a C/C++ combo. I've created a class (in C++)
to encapsulate a particular bit of functionality; this class makes calls to
some C functions in a separate file (nothing complicated). All of the
necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I don't know Anjuta, but I presume that "Build" is trying to link the
project as well as compile it, and I suspect that you're missing an
extern "C" around the header declarations of your C functions. C
doesn't have overloading, so parameters aren't part of the function's
name as seen by the linker. C++ does have overloading, so they *are*
part of the "mangled" linker name.

In short, your C++ compiler is seeing a call to foo(int), thinking
it's a C++ function and telling the linker that it'll need to link to
a function called "foo@int" or somesuch. Your C compiler is compiling
the definition of foo(int), thinking it's a C function and telling the
linker about a function called "foo". The two names don't match up,
hence your problem.

Look up 'extern "C"' in any C++ reference; it should tell you all you
need to know.

cheers,
Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Sheppe
Guest





PostPosted: Fri Aug 27, 2004 3:10 am    Post subject: Re: C++ noob - compiler question. Thanks for all the respons Reply with quote

It ended up being that I had to include the extern "C" lines.

Cheers,

-Sheppe

"Sheppe" <noone (AT) nowhere (DOT) com> wrote

Quote:
I'm not new to programming, but I am fairly new to C++. I've got a bit of
a
problem with a project that I am working on. My environment is as follows:

OS: Mandrake 10
IDE: Anjuta
Compiler: gnu

The project I am working on is a C/C++ combo. I've created a class (in
C++)
to encapsulate a particular bit of functionality; this class makes calls
to
some C functions in a separate file (nothing complicated). All of the
necessary includes are in place.

The problem is this: when I "Compile" my file, it works fine and produces
no
errors, but when I go to "Build" my file I get errors in the file that I
just compiled with no errors. The errors read "undefined reference to
function_name_here(param1*, param2)". The errors are produced on each line
that makes a call to a function in the C file.

I'm a little confused as to what I need to do here. Why does it compile
but
not build?

Thanks.

-Sheppe


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.