2008-09-17 03:06:53     how to compile c and cpp?

Document created by Aaronwu Employee on Aug 7, 2013
Version 1Show Document
  • View in full screen mode

2008-09-17 03:06:53     how to compile c and cpp?

Matthew Wang (CHINA)

Message: 62281   

 

in output_example.h:

pthread_mutex_t mymutex;

 

 

in output_example.c:

 

#include "my_net_transfer.h"

 

extern void my_net_transfer( void );

 

main()

{

pthread_t id;

 

pthread_mutex_lock( &mymutex );

pthread_mutex_init( &mymutex, NULL );

pthread_create( &id, NULL, (void *)my_net_transfer, NULL );

…… ……

pthread_mutex_unlock( &mymutex );

}

 

 

in my_net_transfer.h:

extern "C" void my_net_transfer( void );

 

 

in my_net_transfer.cpp:

#include "output_example.h"

 

void my_net_transfer( void )

{

pthread_mutex_lock( &mymutex );

pthread_mutex_unlock( &mymutex );

}

 

 

Makefile:

NETOBJ = my_net_transfer.o

NETSRC = my_net_transfer.cpp

CXX = bfin-linux-uclibc-g++

 

$(NETOBJ): $(NETSRC)

$(CXX) $(CFLAGSNET) $(INCLD) -c $^

 

output_example$(EXESUF): output_example.o $(NETOBJ)

$(CC) $(CFLAGS) $(LDFLAGS) $(INCLD) -o $@ $< $(EXTRALIBS) -lpthread -l$(LIB_NAME)

 

AS RESULT, make error:

undefined reference to '_my_net_transfer'

collect2: ld returned 1 exit status

 

 

c call cplusplus, is it okay?

 

I used extern "C", but there is also a link error during compling

TranslateQuoteReplyEditDelete

 

 

2008-09-17 03:20:56     Re: how to compile c and cpp?

Mike Frysinger (UNITED STATES)

Message: 62282   

 

you didnt declare extern "C" in the one place that it matters: the C++ file where the function is actually defined

 

QuoteReplyEditDelete

 

 

2008-09-17 07:04:50     Re: how to compile c and cpp?

Matthew Wang (CHINA)

Message: 62306   

 

Hey, I got some further info and I wish to get some support !

 

Here is Makefile:

 

INCLUDE =/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/jrtplib-3.7.1/

install_wangsu/include/jrtplib3

 

LIB_INC=-L/home/seuic/h_SlaveH_CentOS5H2nd/

ip_camera_bf536/jrtplib-3.7.1/install_wangsu/lib

 

LIB_NAME = jrtp

 

INCLD = -I $(INCLUDE)

 

NETOBJ = my_net_transfer.o

 

NETSRC = my_net_transfer.cpp

 

CXX = bfin-linux-uclibc-g++

 

CFLAGSNET += -g -Wall

 

EXPOBJ = output_example.o

 

EXPSRC = output_example.c

 

output_example$(EXESUF): $(NETOBJ) $(EXPOBJ)

@echo ">>>>>>begin to generate output_example<<<<<<"

$(CXX) $(LDFLAGS) -o $@ $< $(LIB_INC) $(EXTRALIBS) -lpthread -l$(LIB_NAME)

 

$(EXPOBJ): $(EXPSRC)

@echo ">>>>>>begin to compile output_example.c<<<<<<"

$(CC) $(CFLAGS) -c $^

 

$(NETOBJ): $(NETSRC)

@echo ">>>>>>begin to compile my_net_transfer.cpp<<<<<<"

$(CXX) $(CFLAGSNET) $(INCLD) -c $^

 

However I got the following error info:

 

>>>>>>begin to compile my_net_transfer.cpp<<<<<<

bfin-linux-uclibc-g++ -g -Wall  -I /home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/jrtplib-3.7.1/install_wangsu/include/jrtplib3 -c my_net_transfer.cpp

 

>>>>>>begin to compile output_example.c<<<<<<

bfin-linux-uclibc-gcc -fomit-frame-pointer -g -Wdeclaration-after-statement -Wall -Wno-switch -Wdisabled-optimization -Wpointer-arith -Wredundant-decls -Wno-pointer-sign -O3 -I"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768" -I"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768" -I"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavutil -I"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavcodec -I"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavformat -I"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libswscale -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_ISOC9X_SOURCE -DHAVE_AV_CONFIG_H -c output_example.c

 

>>>>>>begin to generate output_example<<<<<<

bfin-linux-uclibc-g++ -L"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavformat -L"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavcodec -L"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavutil -rdynamic -export-dynamic -Wl,--warn-common -Wl,--as-needed -Wl,-rpath-link,"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavcodec -Wl,-rpath-link,"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavformat -Wl,-rpath-link,"/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/trunk/lib/ffmpeg/ffmpeg-svn-9768"/libavutil -g -o output_example my_net_transfer.o -L/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/jrtplib-3.7.1/install_wangsu/lib -lavformat -lavcodec -lavutil -lm -ldl -lpthread -ljrtp

/home/seuic/h_SlaveH_CentOS5H2nd/ip_camera_bf536/toolchain/svn-root-linux-uclibc/bin/../bfin-linux-uclibc/runtime/usr/lib/crt1.o: In function `__start':

libc/sysdeps/linux/bfin/crt1.S:137: undefined reference to `_main'

collect2: ld returned 1 exit status

make: *** [output_example] Error 1

 

I wrote a JRTPLIB send function in my_net_transfer.cpp file, and I compiled it with bfin-linux-uclibc-g++, and also I modified the out_example.c in which JRTPLIB send function would be called in FFMPEG sources.

 

I used extern "C" with my_net_transfer(), and compiled it successly, as result , my_net_transfer.o and output_example.o were generated, but when it linked , the error happened.

 

I used bfin-linux-uclibc-g++ to do the linking work, and of course, I linked FFMPEG lib, JRTPLIB lib and pthread lib

 

So according to the error info above, DOES it be correlative to the bfin-linux-uclibc compiler???

 

ANY HELP WILL BE APPRECIATED!!!

TranslateQuoteReplyEditDelete

 

 

2008-09-17 07:09:30     Re: how to compile c and cpp?

Mike Frysinger (UNITED STATES)

Message: 62307   

 

the error message already tells you the problem: you never defined a main() function

 

QuoteReplyEditDelete

 

 

2008-09-17 23:16:15     Re: how to compile c and cpp?

Matthew Wang (CHINA)

Message: 62352   

 

to Mike Frysinger:

 

I defined int main(int argc, char **argv) function.... it was in output_example.c and was compiled to .o file by bfin-linux-uclibc-gcc, and finally I linked output_example.o and my_net_transfer.o by bfin-linux-uclibc-g++

TranslateQuoteReplyEditDelete

 

 

2008-09-18 00:32:13     Re: how to compile c and cpp?

Mike Frysinger (UNITED STATES)

Message: 62353   

 

you're wrongly mixing C and C++ objects.  main() is not an extern C.

QuoteReplyEditDelete

 

 

2008-09-18 05:29:31     Re: how to compile c and cpp?

Matthew Wang (CHINA)

Message: 62376   

 

problem was fixed!

 

the main error was the flags $(LDFLAGS) when linking

 

thank you for your help~~~

Attachments

    Outcomes