Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cintari

macrumors newbie
Original poster
Mar 1, 2007
21
0
I have code that compiles on my linux box that is using gcc 3. I bring the code over to my mac and it fails to link.

Undefined symbols error.

The symbols are clearly defined in my header files but the linker is failing to find them.

Does anyone know how to install gcc 3? I have a new alu macbook.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
I have code that compiles on my linux box that is using gcc 3. I bring the code over to my mac and it fails to link.

Undefined symbols error.

The symbols are clearly defined in my header files but the linker is failing to find them.

Does anyone know how to install gcc 3? I have a new alu macbook.

A quick reality check: What are the chances that there is a bug ni gcc 4.0 that hasn't stopped Apple from delivering Tiger and Leopard, and hasn't stopped hundreds of thousands of developers from writing code for MacOS X, and you are the first one who finds out about it, and going back to gcc 3.0 will fix it?
 

Kane.Elson

macrumors regular
Jul 27, 2006
204
0
I agree with the above poster. GCC 4 has been stable for a long time now, pretty much every modern unix/linux platform has had it installed by default for over a year now. I think if there was such a big linking problem it would have been fixed by now. I have never had any problems compiling in recent builds of gcc 4.
I think you just need to look at what you are doing. Maybe you missed some parameters when compiling or something. A bit has changed since 3....
 

cintari

macrumors newbie
Original poster
Mar 1, 2007
21
0
how do you explain why the exact code compiles using my linux box but fails on linking errors on my mac?

As it turns out, I am not the only person that is in a separate reality than the rest of you.

I found this:
http://www.macosxhints.com/article.php?story=20060423105014540

However, the instructions here do not work. I was hoping someone had a different way of doing it.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
What symbols are not being defined in the source code you're attempting to compile and what command are you using to compile your source?
 

cintari

macrumors newbie
Original poster
Mar 1, 2007
21
0
What symbols are not being defined in the source code you're attempting to compile and what command are you using to compile your source?

Here is a snippet
Code:
Undefined symbols:
  "Player::kShotYOffset", referenced from:
      __ZN6Player12kShotYOffsetE$non_lazy_ptr in simulation.o
      __ZN6Player12kShotYOffsetE$non_lazy_ptr in hud.o

inside player.h
Code:
class Player {
...
	static const float kShotYOffset = 0.05f;
...
};

Here is what my make command is doing.

g++ ${objects} -framework Cocoa -framework OpenGL -framework GLUT -framework Foundation -lstdc++ -lm -shared -o game
 

ncl

macrumors member
Aug 16, 2008
58
0
Here is a snippet
Code:
class Player {
...
	static const float kShotYOffset = 0.05f;
...
};

In C++, static const float data members cannot be initialized in the class definition. You have to separate the declaration and initialization:
In Player.h:
Code:
class Player {
	static const float kShotYOffset;
...
};
In Player.cpp:
Code:
const float Player::kShotYOffset = 0.05f;

I don't know why it worked with gcc 3. AFAIK, it shouldn't.
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
It is highly unlikely that GCC 3 is doing anything correctly. What is probably happening is that GCC 3 allowed a mistake to go unnoticed and they just fixed the bug in GCC 4. So it is just now telling you about the bugs in your code.

This is why you should always compile your code using the pedantic settings :).
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Initialising a static float in the class header is not in the standard. I'm finding that gcc 4 still lets you do it though, but you still need to declare storage for the variable. Perhaps gcc3 was allowing initialisation of floats the same way as ints for convenience?

b e n
 

cintari

macrumors newbie
Original poster
Mar 1, 2007
21
0
In C++, static const float data members cannot be initialized in the class definition. You have to separate the declaration and initialization:
In Player.h:
Code:
class Player {
	static const float kShotYOffset;
...
};
In Player.cpp:
Code:
const float Player::kShotYOffset = 0.05f;

I don't know why it worked with gcc 3. AFAIK, it shouldn't.

Thanks. This worked. It was lucky there weren't too many of these in the code. I should note that if you define it in the .h file and define it again in a .cpp file, the compiler complains of a double definition. So it seems that the compiler does store a value for it while parsing. It must ignore it during code generation.

ALSO, I found out that Apple does not support gcc 3 compiled programs for intel macs.

http://discussions.apple.com/thread.jspa?messageID=6900842
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.