LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-12-2010, 09:50 PM   #1
eryn
Member
 
Registered: Jul 2009
Posts: 43

Rep: Reputation: 15
Unsuccessful debug using Makefile during integration of cpp code to c code


Hello everyone,

We have codes written in C language and C++ language.

We have successfully wrote wrapper code and debug using manual command and the code runs good.
Now, we need to eliminate this manual debug step by inserting them into Makefile.

Before that, let me explain the flow.

Our main running code is called core.c. In core.c, we called a function named ARMWashCycle(). This function is written and saved as arm.c. In function ARMWashCycle(), we called functions from cpp code and we did a wrapper code for this.

Here is our arm.c
Code:
#include <stdio.h>
#include <unistd.h>
#include "wrapper.h"
#include "arm.h"

int ARMWashCycle ( )
{
        int arm=0;

        libInit();
        libClose();

        return 0;
}
In core.c, we did include the arm.h header. This code is debugging fine when we do manual debug using the following command.

Step 1:
Code:
g++ -I<include path needed to compile cpp code> -c -DNUM_DRIVES=6 wrapper.cpp
Step 2:
Code:
gcc <CFLAGS> <PKG_CONFIG> -c core.c motor.c <all sub-code needed....>
Step 3:
Code:
g++ -o core <all object files needed> -lusb <Linker Flags> /home/user/rwc/Source/cmdapi/lib/libRWCCommanderApi.so.1.0.0 /home/user/rwc/Source/lib/lib/libBRBTeam.so.1.0.0
These will debug our code successfully and we can run the code.

Now, when we try to do the Makefile for this,

Makefile:
Code:
#------------------------------------------------------------------------------
# Linker Flags
#------------------------------------------------------------------------------
LDFLAGS := -L../lib
ifeq ($(OS),Linux)
        LDFLAGS += -L/usr/local/lib -lusb-1.0 -lm -lm /home/user/Source/cmdapi/lib/libRWCCommanderApi.so.1.0.0 -lm /home/user/Source/lib/lib/libBRBTeam.so.1.0.0

else
        LDFLAGS += -L../libusb/libusb-win32-src -lusb
endif

#------------------------------------------------------------------------------
# Common rules
#------------------------------------------------------------------------------
%.o:    %.c
        $(CC) -c $(CFLAGS) -o $@ $<


#------------------------------------------------------------------------------
# Target Builds
#
#------------------------------------------------------------------------------
wrapper.o : wrapper.cpp
        g++ -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I../lib/include -I.moc -I. -c -DNUM_DRIVES=6 wrapper.cpp

arm.o : arm.c
        g++ -Wall -c arm.c

core : core.o motor.o switch.o analog.o echoclnt.o helper.o mapping.o wrapper.o arm.o
        $(CC) -o $@ $^ -lsub $(LDFLAGS)


clean:
        rm -f *.o *.out *.err *.exe *.a *.so core
Then, it will give us the following error:
Code:
core.c: In function ‘main’:
core.c:160: warning: implicit declaration of function ‘ARMWashCycle’

core.o: In function `main':
/home/user/rwc/core/core.c:160: undefined reference to `ARMWashCycle'
collect2: ld returned 1 exit status
make: *** [core] Error 1
Anyone knows how can we solve this?
We have tried include the library path in /etc/ld.so.conf and run "ldconfig". It returns the same result.

When we close the function call of ARMWashCycle() in core.c, the debug will be successful.

Last edited by eryn; 05-13-2010 at 02:48 AM. Reason: incomplete post
 
Old 05-13-2010, 01:56 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by eryn View Post
Code:
core.c: In function ‘main’:
core.c:160: warning: implicit declaration of function ‘ARMWashCycle’

core.o: In function `main':
/home/user/rwc/core/core.c:160: undefined reference to `ARMWashCycle'
collect2: ld returned 1 exit status
make: *** [core] Error 1
First, /etc/ld.so.conf and ldconfig are to do with run-time linking - what you have here is a link-time problem, so fiddling with them won't help one bit.

Secondly, case matters in C - your code defines ArmWashCycle but then later references ARMWashCycle.

But what you REALLY want to do is to have used -Werror so the first error stops you in your tracks and you're forced to fix it, meaning you'll never get distracted by the second error.
 
Old 05-13-2010, 02:51 AM   #3
eryn
Member
 
Registered: Jul 2009
Posts: 43

Original Poster
Rep: Reputation: 15
About the case matter, we have checked in the program, it is 100% identical. Just typo error when tried to post a new thread.

The first is not really an error, it is just a warning. Code can still be debug successfully with warning, right?
 
Old 05-13-2010, 12:12 PM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by eryn View Post
The first is not really an error, it is just a warning. Code can still be debug successfully with warning, right?
That's correct - but I wouldn't advise it. You'll thank yourself later. -Werror is your friend, as is -Wall.

Anyway, in that case your problem is that you use g++ to compile arm.o, but $(CC) to compile core, and the name mangling is what messes things up. Switch to using only a C compiler or a C++ compiler, or use extern "C" if you want to mix.
 
Old 05-13-2010, 09:36 PM   #5
eryn
Member
 
Registered: Jul 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you, John.
We solved the problem with extern "C"
Here is what we have modified.

In arm.h
Code:
#ifdef __cplusplus
extern "C"
{
#endif

#include<stdio.h>
#include<unistd.h>
#include "wrapper.h"

int ARMWashCycle ( );
#ifdef __cplusplus
}
#endif

Last edited by eryn; 05-13-2010 at 09:36 PM. Reason: typo error
 
  


Reply

Tags
code, cpp, debug, error, function, makefile, reference, undefined


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call other executables from cpp code er.saurabh Linux - Newbie 2 01-18-2010 08:23 AM
[SOLVED] How to call cpp library function in c code? eryn Programming 19 12-07-2009 01:41 AM
compile cpp code including iostream ufmale Programming 1 09-13-2008 01:30 AM
about `make` and debug source code naihe2010 Programming 9 12-13-2005 05:03 AM
debug problem w/ c++ code nub47 Programming 2 12-17-2003 12:31 AM


All times are GMT -5. The time now is 05:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration