-1

I am modifying the linux kernel for a device locally and I added a header file, say a.h inside /include/linux and referenced it in two C files inside the kernel directory. The header file contains a structure which has been initialised there itself. Whenever I compile the kernel, it shows me a multiple definition of 'struct_var' error. What changes should I make?

  • You need to ensure the initialisation only happens once (not in the header file). For a specific answer you’d need to edit your question to include the declarations in question. – Stephen Kitt Jun 7 '17 at 13:29
1

You need to ensure that it is only included once.

Typically, you would use

#ifndef UNIQUE_IDENTIFIER_MAYBE_THE_FILENAME
#define UNIQUE_IDENTIFIER_MAYBE_THE_FILENAME

// your file contents go here

#endif

That way, the second time the compiler includes this file, UNIQUE_IDENTIFIER_MAYBE_THE_FILENAME is defined, so it does not define any of the bits inside.

  • Thanks, but I had already tried this and this hadn't worked! – Vishal Khopkar Jun 7 '17 at 17:40
  • That is strange. This method has been used for over 20 years to stop multiple definitions. We may need more information, or an example to help more. – Xetius Jun 14 '17 at 8:59
  • No issues, I found out a way; thanks for your help! – Vishal Khopkar Jul 14 '17 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.