The International Simutrans Forum

 

Author Topic: [SOLVED][RC 11.9001] Linker and simcity.cc error  (Read 3623 times)

0 Members and 1 Guest are viewing this topic.

Offline Vonjo

  • *
  • Posts: 273
[SOLVED][RC 11.9001] Linker and simcity.cc error
« on: March 27, 2013, 08:04:59 AM »
I got ld error while compiling RC 11.9001 no-graphics (see attachment). Graphics version doesn't have this problem.
---
I also got this error while compiling with gcc 4.6:
Code: [Select]
simcity.cc: In member function 'void stadt_t::check_all_private_car_routes()':
simcity.cc:2505:102: error: taking address of temporary [-fpermissive]
simcity.cc:2505:109: error: taking address of temporary [-fpermissive]
-fpermissive flag can be used as temporary solution though.
« Last Edit: March 31, 2013, 01:30:21 PM by Vonjo »

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #1 on: March 29, 2013, 01:14:06 PM »
Vonjo,

thank you for your report. Having looked carefully at the relevant section of the code, taking the address of a temporary here is entirely proper and intended, as the method to which it is passed only uses it internally for the duration of the method, and does not store it for future use (and furthermore, this characteristic is fundamental to the method). I suggest that the -fpermissive flag is always set when compiling for GCC.

Indeed, this appears to be set in config.default:

Code: [Select]
# Following flags exists
# DOUBLE_GROUNDS: Enables two height tiles
# HALF_HEIGHT: Enables half height tiles (8 pixel instead 16)
# OTTD_LIKE: Enables half height tiles and crossconnected industries; defaul folder pak.ttd/
# DESTINATION_CITYCARS: Citycars can have a destination
# USE_C: no assembler for copying
# BIG_ENDIAN: MUST by set for PPC/Motorola byte order! (old mac, amiga)
# COMMAND_LINE_SERVER: No graphics, use only for network servers. Must also set COLOUR_DEPTH = 0.
FLAGS = -DNDEBUG -fpermissive

Did you perhaps delete by accident the -fpermissive (and perhaps also) -DNDEBUG lines when you added COMMAND_LINE_SERVER to your flags?

Offline Vonjo

  • *
  • Posts: 273
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #2 on: March 29, 2013, 02:15:52 PM »
Thank you for the explanation. I was just worried that it may cause problems in the future. I'll keep the -fpermissive flag then.
Did you perhaps delete by accident the -fpermissive (and perhaps also) -DNDEBUG lines when you added COMMAND_LINE_SERVER to your flags?
I was just lazy. I copied config.default from Simutrans standard. I thought it was just the same. ;D
Why do you need -DNDEBUG while it has been available in Makefile?

---
How about the ld error?
« Last Edit: March 29, 2013, 02:21:21 PM by Vonjo »

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #3 on: March 29, 2013, 02:46:02 PM »
The ld error - I thought that that was the same issue as was solved by the -fpermissive flag? I am a little confused.

The -DNDEBUG line ensures that, for a release build, the DEBUG precompiler directive is not set, which disables assertions and other debugging code that can slow the game and, in some cases, put what is for ordinary play superfluous information in the GUI. I strongly recommend that you use the Simutrans-Experimental specific makefile.

Offline MCollett

  • *
  • Posts: 214
  • Languages: en
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #4 on: March 29, 2013, 09:07:07 PM »
Having looked carefully at the relevant section of the code, taking the address of a temporary here is entirely proper and intended

Taking the address of a non-lvalue is undefined behaviour according to the C++ standard.  There is absolutely no guarantee that it will do what you think, or indeed anything even vaguely sensible.  In practice, it will be particularly dangerous as soon as you are compiling with any significant level of optimisation - it can result in one of those nasty bugs that doesn't show up when debugging, but causes random crashes in the released version.

(I almost mentioned this particular problem in the thread I started about compiling with clang, but in the end just cheated and turned the error off.)

Best wishes,
Matthew

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #5 on: March 29, 2013, 09:31:15 PM »
Hmm - I didn't think that a temporary variable counts as a non-lvalue? Surely if I write:

Code: [Select]
int a;
int* b = &a;
some_method(b);

I am taking the address of a temporary? This is a legitimate thing to do, is it not, where we know that some_method() does not store b for later use?

Or is the problem that I put the constructor itself in the method call, rather than a separate variable? Is the following a problem?

Code: [Select]
class some_class
{
int a;
int b;
some_class::some_class(int x, int y) { a = x; b = y; }
};

void other_class::method()
{
random_method(&some_class new_object(1, 2));
}

Would it solve the problem to replace void other_class::method() with the following?

Code: [Select]

void other_class::method()
{
some_class new_object(1,2);
random_method(&new_object);
}

Or is it really necessary to use heap allocation like this?

Code: [Select]
void other_class::method()
{
some_class *new_object;
new_object = new some_class(1,2);
random_method(new_object);
delete new_object;
}

Offline MCollett

  • *
  • Posts: 214
  • Languages: en
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #6 on: March 30, 2013, 01:18:30 AM »
Surely if I write:

Code: [Select]
int a;
int* b = &a;
some_method(b);

I am taking the address of a temporary?

No, a is not a temporary.  Even just some_method(&a) should be OK.

Quote
Or is the problem that I put the constructor itself in the method call, rather than a separate variable?

Yes, exactly.  If there is not a separate variable, the compiler is free to put the temporary into a register, or to optimise its existence away altogether.  In either case trying to take its address causes problems.

Quote
Would it solve the problem to replace void other_class::method() with the following?

Code: [Select]

void other_class::method()
{
some_class new_object(1,2);
random_method(&new_object);
}
That should do.  It forces the compiler to allocate real memory on the stack.  (Or more precisely, to behave as if it does: a sufficiently smart global optimiser is still free to eliminate the variable, but it is then the compiler's responsibility to avoid nasal demons, not yours.)

Best wishes,
Matthew

Offline Vonjo

  • *
  • Posts: 273
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #7 on: March 30, 2013, 06:56:49 AM »
The ld error - I thought that that was the same issue as was solved by the -fpermissive flag? I am a little confused.
No, LD error is another unrelated error. It only happens with no-graphic version (BACKEND = posix, COLOUR_DEPTH = 0, OSTYPE = linux). I'm using Simutrans experimental Makefile. It still happens with 11.9002.
« Last Edit: March 30, 2013, 07:42:38 AM by Vonjo »

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #8 on: March 30, 2013, 11:31:04 AM »
Ahh - you need to add the flag "COMMAND_LINE_SERVER" when you set COLOUR_DEPTH=0. Try it with that and see how you get on.

Matthew: thank you: that is very helpful.

Offline Vonjo

  • *
  • Posts: 273
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #9 on: March 30, 2013, 12:17:25 PM »
Ahh - you need to add the flag "COMMAND_LINE_SERVER" when you set COLOUR_DEPTH=0. Try it with that and see how you get on.
Oh, I didn't notice that. Thank you. I'll try it. :)

Offline isidoro

  • Devotee
  • *
  • Posts: 1137
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #10 on: March 31, 2013, 01:00:37 AM »
If there is such a dependency, it can be enforced in the Makefile, can't it?

Something like: if COLOUR_DEPTH is defined and is 0, then define COMMAND_LINE_SERVER

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #11 on: March 31, 2013, 01:09:18 AM »
Hmm, that's not a bad idea. I don't know makefile syntax well enough to code this - can you suggest appropriate syntax for this?

Offline Vonjo

  • *
  • Posts: 273
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #12 on: March 31, 2013, 01:29:15 PM »
Ahh - you need to add the flag "COMMAND_LINE_SERVER" when you set COLOUR_DEPTH=0. Try it with that and see how you get on.
It works now. Thanks. :)

Offline isidoro

  • Devotee
  • *
  • Posts: 1137
Re: [SOLVED][RC 11.9001] Linker and simcity.cc error
« Reply #13 on: March 31, 2013, 11:50:24 PM »
Hmm, that's not a bad idea. I don't know makefile syntax well enough to code this - can you suggest appropriate syntax for this?

How is COMMAND_LINE_SERVER defined in the config file?  Is it a stand alone variable (COMMAND_LINE_SERVER=0) or is it a flag added to the FLAGS variable (-DCOMMAND_LINE_SERVER)?

Offline Vonjo

  • *
  • Posts: 273
Re: [RC 11.9001] Linker and simcity.cc error
« Reply #14 on: April 01, 2013, 06:23:25 AM »
Hmm, that's not a bad idea. I don't know makefile syntax well enough to code this - can you suggest appropriate syntax for this?
This works for me:
Code: [Select]
ifeq ($(COLOUR_DEPTH),0)
  CFLAGS += -DCOMMAND_LINE_SERVER
endif
Put that code anywhere before the SOURCES part.
« Last Edit: April 01, 2013, 07:06:41 AM by Vonjo »

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [SOLVED][RC 11.9001] Linker and simcity.cc error
« Reply #15 on: April 01, 2013, 01:11:09 PM »
Splendid - thank you!

Offline isidoro

  • Devotee
  • *
  • Posts: 1137
Re: [SOLVED][RC 11.9001] Linker and simcity.cc error
« Reply #16 on: April 01, 2013, 11:31:17 PM »
I would check if COMMAND_LINE_SERVER is already defined.  You can even add a warning then:
Code: [Select]
ifeq ($(COLOUR_DEPTH),0)
  ifeq ($(findstring -DCOMMAND_LINE_SERVER,$(FLAGS)),)
    $(warning If COLOUR_DEPTH is 0, COMMAND_LINE_SERVER must be defined.  Fixing it...)
    FLAGS += -DCOMMAND_LINE_SERVER
  endif
endif

Or, alternatively, produce an error, if you think that it should be solved by the user:
Code: [Select]
ifeq ($(COLOUR_DEPTH),0)
  ifeq ($(findstring -DCOMMAND_LINE_SERVER,$(FLAGS)),)
    $(error COLOUR_DEPTH is 0 and COMMAND_LINE_SERVER isn't defined.  Aborting...)
  endif
endif

I haven't checked the code...  It can have an error, though...


Offline jamespetts

  • Simutrans-Extended project coordinator
  • Moderator
  • *
  • Posts: 20274
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: [SOLVED][RC 11.9001] Linker and simcity.cc error
« Reply #17 on: April 02, 2013, 10:33:39 PM »
These changes are now on Github.