View topic - Momentics IDE - "undefined reference" for function

Momentics IDE - "undefined reference" for function

anything that doesn't fit to other groups.

Momentics IDE - "undefined reference" for function

Postby tappocbm » Fri Apr 09, 2010 7:39 pm

Hi everyone,
I am new to Momentics, but got it installed on a Linux machine and used the "10-steps to your first qnx program" to write, compile and debug a simple hello world application.

Now here is the problem:
As soon as I don't write my code in the autogenerated helloworld.cc, but put functions in a separate file (.c and .h), I get the following error:

undefined reference to 'add(float, float)'

I added two simple files to the project, test.c and test.h, which look like this:
test.h:
float add(float a, float b);

test.c:
float add(float a, float b) {return a+b;}

The files are both in the main project folder (like helloworld.cc) and show up on the left side in the project list view as part of the project, but it seems like Momentics is still not using the test.c file in the project.

I tried to look for any setting in the project properties where I have to define the files I want to make into my project, but I can't find anything. I have the same problem when I use Momentics in Windows.


This is probably really stupid, but I can't figure it out. So any help would be great.

Many thanks,
Tappo
tappocbm
New Member
 
Posts: 1
Joined: Fri Apr 09, 2010 7:30 pm

RE: Momentics IDE - "undefined reference" for func

Postby udam_u » Fri Apr 09, 2010 9:55 pm

Are you sure that you have got the same function name in test.h and test.c?
They are probably different. Check function name, add new line to all project files, save and try to recompile.
:wink:
udam_u
New Member
 
Posts: 5
Joined: Wed Mar 24, 2010 11:15 am

RE: Momentics IDE - "undefined reference" for func

Postby juanplacco » Sat Apr 10, 2010 4:00 am

Did you #include "test.h" in test.c ?
juanplacco
Senior Member
 
Posts: 318
Joined: Wed May 17, 2006 2:30 pm

RE: Momentics IDE - "undefined reference" for func

Postby mario » Sat Apr 10, 2010 2:00 pm

If your file is helloword.cc and test.c then when helloworld.cc use the prototype in test.h it assumes add() is a C++ fonction and apply the name mangling rule to it that explain why it can find it at link time. You need to tell helloworld.cc add() is C and not C++ by having test.h look like this

#ifdef __cplusplus
extern "C" {
#endif

float add(float,float);

#ifdef __cplusplus
}
#endif

If this is the solution to your problem note that this has nothing to do with Momentics or QNX. It a C/C++ issue.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron