[NSWI004] Linker problem

Vojtech Horky horky at d3s.mff.cuni.cz
Thu Nov 5 16:12:02 CET 2020


Hello.

Dne 05. 11. 20 v 15:13 Georgii Ekserdzhian napsal(a):
> Good afternoon,
> 
> I declared a list that would contain the threads in scheduler.h, but for 
> some reason when I compile the kernel, I get an error message saying 
> that this list is defined in multiple places.
> Can you please tell me what am I doing wrong?
> I pushed my code into my team's repo(ekserdzg branch).

You should not declare any "plain" variables in header files. They 
should be either static in the respective C file or declared "extern".

When you declare them "extern", you also need to declare them in the C 
file and the "extern" only tells the linker that the list exists and is 
defined elsewhere (and only once).

You current code created the list multiple times and the linker was 
confused which definition is the "correct" one.

As a general recommendation: prefer to not have any globally visible 
variables, hide them inside your C files and expose them via functions.

The split between thread.c and scheduler.c is kind of a hint that they 
actually do not need to share any internal state (except the common 
structure of thread_t with list links).

As a side note: consider naming the list either all_threads or 
ready_threads, i.e. something more descriptive.

Hope this helps,
- VH


More information about the NSWI004 mailing list