سؤال

I am compiling a c++ application for the STM32F4 microcontroller. All my code compiles successfully, but the link step fails with the following error:

/tmp/ccGj7Wge.o: In function `LoopFillZerobss':
(.text.Reset_Handler+0x32): undefined reference to `main'
collect2: error: ld returned 1 exit status

I am familiar with typical cause for undefined reference to main errors, that is, trying to compile an application without a main present at all. However, I do have a main.cpp, and naturally, a main function within it. It compiles to main.o and this is linked in with the other object files created during compilation.

I am cross-compiling onto the STM32F407 and I'm running bare metal.

Anyone have any insight into this or seen this error before? The only place I see the LoopFillZerobss function is in the assembly startup file for the board.

هل كانت مفيدة؟

المحلول

Things to check:

  • Make sure you declare main correctly. For example, int main(void) or int main(int argc, char **argv), and not void main(void). This Stack Overflow article does a great job: What is the proper declaration of main?
  • Make sure you haven't accidentally wrapped main in a namespace.
  • If you have an exceptionally quirky C++ compiler, you may need to mark main as extern "C", but to be quite honest, I've never seen tools that needed that.

You should be able to see what symbols got defined by your main.o by running a tool such as nm on it. (At least, UNIX-style tool chains, such as the GNU tool chain, offer an nm tool.) nm will list the set of symbols defined by your executable. You should see main listed as-is.

For example, on my Linux box, int main(void) { } in an otherwise empty .cpp file results in the following output from nm:

$ nm main.o
                 U __gxx_personality_v0
0000000000000000 T main

If I wrap main in a namespace (in this case namespace fred), I get a mangled name like this:

$ nm main.o
0000000000000000 T _ZN4fred4mainEv
                 U __gxx_personality_v0

Notice the extra gobbledegook around the name main. You shouldn't see such gobbledegook in your nm output.

You should be able to see if you're correctly declaring main and not accidentally dropping it into a namespace. Once you get that sorted, then the initialization routine LoopFillZerobss should be much happier.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top