Christopher said:
Hello again, for clarification purposes, I would like to restate my
question!
When I link a cc compiled file with a CC compiled file, I am told the
c routine is undefined and I do not get a "a.out"! What can/should I
do? Here's an example consisting of two files followed by their
compilation :
/* -------- cut here file ta.c : --------- */
void test (int t);
main ()
{
int my_t;
my_t = 3;
test (my_t);
}
/* -------- cut here file tb.c : --------- */
#include <stdio.h>
void test (int t)
{
printf ("t is %d\n",t);
}
/* -------- cut here compilation : --------- */
%cc -c tb.c
No such feature exists (-5,116)
%CC ta.c tb.o
No such feature exists (-5,116)
ld32: ERROR 33 : Unresolved text symbol "test(int)" -- 1st
referenced by ta.o.
Use linker option -v to see when and which objects, archives
and dsos are loaded.
ld32: INFO 152: Output file removed because of error.
Thank you,
Christopher Lusardi
P.S.: My initial results when I search google is very negative?
I believe this is a severe case of language mixup.
I compiled your files using GNU C compiler, "gcc" with no errors:
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ gcc -c tb.c
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ gcc ta.c tb.o
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ ls
a.exe ta.c tb.c tb.o
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ ./a.exe
t is 3
However, when I use the GNU C++ compiler, "g++" to compile
the ta.c file and link in the tb.o file, I get a similar
error to yours:
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ g++ ta.c tb.o
/cygdrive/c/DOCUME~1/TH009MA/LOCALS~1/Temp/ccwOY11k.o(.text+0x2c):ta.c:
undefined reference to `test(int)'
collect2: ld returned 1 exit status
I assume that on your system, "cc" is the C compiler and
"CC" is the C++ compiler.
The issue boils down to "name mangling". This is the art of
naming a function so it includes type information to support
function overloading. Research "name mangling" in the FAQ
and in this newsgroup.
You have two solutions: Stick with one compiler and one
language. Don't mix the two.
Or if you insist on mixing the languages, research and
understand 'extern "C"'. This tells the compiler that the
external reference has "C" language linkage (i.e. no
name mangling). Change file ta.c to:
extern "C"
{
void test (int t);
}
int main (void)
{
int my_t;
my_t = 3;
test (my_t);
}
*********************************************
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ cat ta.c
extern "C"
{
void test (int t);
}
main ()
{
int my_t;
my_t = 3;
test (my_t);
}
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ g++ ta.c tb.o
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ ./a.exe
t is 3
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book