Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Link problems - undefined reference with ternary operator

Posted on 2006-03-30
18
Medium Priority
?
249 Views
Last Modified: 2008-02-01
I am receiving the following error during linking of my program (with g++ 2.96, RedHat 8, kernel 2.4):

/tmp/ccAC0qDg.o(.text+0x129a): In function `StartHardwareThread(void *)':
: undefined reference to `Serial::Baud9600'
/tmp/ccAC0qDg.o(.text+0x12a5): In function `StartHardwareThread(void *)':
: undefined reference to `Serial::Baud38400'


The line of code that this relates to is the following:
   Serial::BaudRate baud = (m_config.find("FastPSoC") == m_config.end()? Serial::Baud9600 : Serial::Baud38400);

The strange thing is, the following line does *not* cause the problem:
   Serial::BaudRate baud = (true? Serial::Baud9600 : Serial::Baud38400);
But this does cause the problem:
   bool thisIsTrue(true);
   Serial::BaudRate baud = (thisIsTrue? Serial::Baud9600 : Serial::Baud38400);
The problem does not occur if I use if...else to control the assignment to variable baud

It seems as if the conditional usage prevents it from linking in properly.
The Serial class is defined in a header file, object code is in a dynamic library (.so for *nix users, .dll for windows users)

Can anyone tell me why this happens?
0
Comment
Question by:boycy
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 2

Expert Comment

by:bachra04
ID: 16335502
I think you must define baud variables first and then conditional usage.

like this

   Serial::BaudRate baud = NULL; // or whatever correct
   
   then add the following line:
   
   baud = (true? Serial::Baud9600 : Serial::Baud38400);
0
 
LVL 2

Expert Comment

by:bachra04
ID: 16335621
Execuse me  I mean:
 
   Serial::BaudRate baud = NULL; // or whatever correct
   
   then add the following line:
   
   baud = (m_config.find("FastPSoC") == m_config.end()? Serial::Baud9600 : Serial::Baud38400);
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 16335777
I have a couple of wild guesses.  Maybe one will work for you.

   Serial::BaudRate baud = (m_config.find("FastPSoC") == m_config.end()? (Serial::Baud9600) : (Serial::Baud38400));

   Serial::BaudRate baud = (m_config.find("FastPSoC") == m_config.end()? (::Serial::Baud9600) : (::Serial::Baud38400));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:boycy
ID: 16340432
Thanks both for replies - unfortunately neither of them worked :'(

I don't understand what's happening here. Obviously Baud9600 and Baud38400 are defined in the library I'm linking to, their identifiers must be included in the preprocessing/compilation stages otherwise it wouldn't even get to linking. So logically, for some reason the identifiers the linker is looking for are not the same as the identifiers stored in the library....depending on how I use them.
Ok bit more to go on:

   Serial::BaudRate baudLow = Serial::Baud9600;
   Serial::BaudRate baudHigh = Serial::Baud38400;
   Serial::BaudRate baud = (g_config.find("FastPSoC") == g_config.end()? baudLow : baudHigh);

that works fine, as does swapping *one* of baudLow / baudHigh in the last line for Baud9600 / Baud38400 respectively. Swapping both of them does not work. Surely this must be some sort of bug in the linker?
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 16343062
What are the exact types of Serial::Baud9600 and 38400?  How is Serial::BaudRate defined?
0
 
LVL 4

Expert Comment

by:as2003
ID: 16354157
class Serial
{
public:
   typedef unsigned int BaudRate;
   static const BaudRate Baud9600 B9600;
   // ...
}
0
 
LVL 6

Author Comment

by:boycy
ID: 16354163
Oops, sorry that post was mine -- shared computer :-?
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 16361961
Try compiling your .cc to a .o file (instead of directly to an executable) and run "nm -C" on the resulting .o.  

I'm thinking you really have a compiler bug here.  Is there a reason you're using g++ 2.96?  Maybe a newer version will not have this problem.  
0
 
LVL 6

Author Comment

by:boycy
ID: 16434549
@Nova: which .cc - the one attempting to use the BaudRate constants?
Unfortunately yes there is a reason - the target is embedded Linux and compiling with anything other than those in gcc 2.96 results in link errors on target (as I recall , it attempts to dynamically link to non-existant GLIBC stuff).
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 500 total points
ID: 16436519
I think you should just write it off as a compiler bug and work around it then.  You're stuck with this compiler and you have a good workaround.  There's no real upside to narrowing down the problem.
0
 
LVL 6

Author Comment

by:boycy
ID: 16707994
as2003 is not my account, I happen to know the guy and used his computer to post a comment, forgetting to log him out and myself in.
0
 
LVL 4

Expert Comment

by:as2003
ID: 16708107
Hello,

Boycy just pointed out your comments GranMod. I can assure you we are two separate entities.

Don't closing either of our accounts. I've put a lot of effort into answering PHP questions. I'll be furious if my progress towards becoming an PHP Expert is erased.

We are good friends and once lived together. He once posted from my computer without logging me out first.

Check out the geographical location of our IPs. You'll notice it's impossible for boycy to travel 200 miles in 1 hour.

Check out our histories. We are interested in totally different topics, our writing style is different, our locations are different. etc. etc.
0
 
LVL 6

Author Comment

by:boycy
ID: 16708167
I could be using a proxy? Yeah, didn't think of that did you! Anyway I'm not.
0
 
LVL 4

Expert Comment

by:as2003
ID: 16708227
Arrr, you got me! I *am* Boycy! hahaha!!! And I hereby confess that I smell of mouldy cheese and like to sniff ladies' shoes.
0
 
LVL 6

Author Comment

by:boycy
ID: 16708247
Umm...yeah ok I thnik we've established who's who now....further to earlier points I'd like to dissassociate myself from him to prevent any further embarassment...

While I'm here, NovaDenizen - thanks for your assistance on the issue. Whilst it remains unsolved, you can take the points. I will have to assume it is a compiler bug.
Cheers,
--Rob
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question