0

I am having trouble compiling the demonstration code for the STM32F4-Discovery using the arm-none-eabi toolchain. The error is occuring in linking and this is the [first] error I'm receiving.

template/obj/stm32f4xx_it.o: In function `OTG_FS_IRQHandler':
template/src/stm32f4xx_it.c:192: undefined reference to `hpcd'

I've been trying to figure this out on and off for several months so I've created this github repository to track the project. The variable hpcd is defined in template/src/usbd_conf.c. My knowledge of C/C++ is unfortunately full of holes, so I'm hoping someone may help me both narrow down the problem and the suggest the proper way to fix it.

I have two questions.

  1. hpcd is included in the file usbd_conf.c. It is used in many files, but none of these files actually include usbd_conf.c, only usbd_conf.h. The question is, by only including the header, can a file which declares a variable as extern PCD_HandleTypeDef hpcd; use the variable without specifically including the .c file?
  2. Assuming the answer to question 1 is yes, why is it the function OTG_FS_IRQHandler isn't finding hpcd? The chain of includes from stm32f4xx_it is main.h -> usbd_core.h -> usbd_conf.h.

I'm assuming the answer to question 1 is yes because this is the demonstration code I believe was running on the board when I purchased it. I can only assume the poblem is with the way I'm building it. Would anyone be willing to help me troubleshoot this problem? I would be happy to provide more details if relevant or to better structure this question, but I'm trying to avoid posting a full USB stack in my question. Thank you.

Mirakurun
  • 4,859
  • 5
  • 16
  • 32
user2027202827
  • 1,234
  • 11
  • 29
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – melpomene Jun 26 '16 at 00:30
  • http://stackoverflow.com/a/12574400/1848654 – melpomene Jun 26 '16 at 00:30
  • As I explained in the question, this is demonstration code provided by ST Microelectronics, so I am inclined to believe that my problem is not related to incorrect code, but instead to the method I'm using to build it. Also, templates are a C++ construct and, as far as I'm aware, don't apply to C code. Please let me know if I'm not understanding what you're implying here @melpomene. – user2027202827 Jun 26 '16 at 00:42
  • 1
    @hobenkr, note that the `hpcd` thing is NOT instantiated in `template/src/usbd_conf.c`; `extern` means it declares a symbol to reference there. Something needs to instantiate an actual `hpcd` object. I think the links provided by @melpomene will provide for good reading to basically understand how c builds work :). As for this particular issue, file `stm32f4xx_hal_pcd.c` has a comment that states: `Declare a PCD_HandleTypeDef handle structure, for example: PCD_HandleTypeDef hpcd;` and more stuff, which implies that you need to create an `hpcd` in your source, I would think. – mkal Jun 26 '16 at 04:17
  • @mkal, the first line of `template/src/usbd_conf.c` following the include statements is `PCD_HandleTypeDef hpcd;`. Is this what you mean by `hpdc` needing to be 'instantiated', because it seems to me that it is. You're correct that the page posted by melpomene is good reading though :) Seems to be full of things that I don't know very well. – user2027202827 Jun 26 '16 at 20:29
  • @hobenkr, blah, sorry I was looking in the wrong file. In that case, then the project isn't building that file (obviously). Check to see if the appropriate driver files are in your project file (.uvproj file if you're using uVision); the IDE uses that to know what to build. – mkal Jun 26 '16 at 22:51
  • I do have an object file for usbd_conf.c. I'm using the gcc toolchain for arm so no uVision stuff. I imagine the problem must be with the makefile since this is the demonstration code provided by STM that was running on the board when I purchased it. – user2027202827 Jun 26 '16 at 23:11

1 Answers1

2

It might help if you put extern PCD_HandleTypeDef hpcd; in one of the headers that gets included by everyone involved (main.h would do, I think).

Example for how the extern stuff works normally:

A file that does define a variable you want to use elsewhere contains

include "a_header.h";
// ...
int some_variable = 42;

The header a_header.h contains

extern some_variable;

The file where you want to use that variable contains

include "a_header.h";
extern some_variable;
//...
foo = some_variable;
some_variable = 47;

such that the linker knows excactly what to do with it.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • I tried your suggestion of including `extern PCD_HandleTypeDef hpcd;` as the first line following the include statements of `main.h` but still have the same linking error. As a side note, the file that the linking error is occuring in (`template/src/stm32f4xx_it.c`) already has that exact statement as the first line following the includes. – user2027202827 Jun 26 '16 at 20:33
  • No, not there, *in* a header that gets included by everyone, e.g.: `main.h`. Sorry for the misunderstanding. – deamentiaemundi Jun 27 '16 at 00:33
  • So you're suggesting creating a new header file containing `extern PCD_HandleTypeDef hpcd;` and include it in any file I seem to have problems with? What confuses me about this solution is the file I'm having trouble with (`template/src/stm32f4xx_it.c`) has `extern PCD_HandleTypeDef hpcd;` as the first line following its' `#include`s, so I would think this would not solve the problem for the file where this seems to be the problem. Does that make sense? – user2027202827 Jun 27 '16 at 00:37
  • No, put the line `extern PCD_HandleTypeDef hpcd;` somewhere into `main.h`, if 'main.h' gets included everywhere where `hpcd` gets used, otherwise put it into a header that does. – deamentiaemundi Jun 27 '16 at 00:39
  • Shouldn't that already be covered though with that line being included in `stm32f4xx_it.c`? As a side not, I've already tried including that line in `main.h` and it didn't solve the problem. – user2027202827 Jun 27 '16 at 00:47
  • I added some explanation, got it now? – deamentiaemundi Jun 27 '16 at 00:52
  • !! Yes, I get it now. I didn't realize extern had to be used in both the source and destination file. I added `extern PCD_HandleTypeDef hpcd;` to `usbd_conf.c` and no longer get that error. Thank you. – user2027202827 Jun 27 '16 at 00:59