0

I have implemented a set of functions satisfying the posix interface and want to run it on my STM32 development board. However, during the compilation process, it was found that the declared structure was duplicated with the structure name in the cross-compilation toolchain. For example, they are all called pthread_attr_t, and pthread_attr_t in the cross-compilation toolchain is automatically used during compilation, which leads to program errors. How should I solve this problem so that my program correctly references the pthread_attr_t I declared myself.

I can't change the name of my structure, and I also tried changing #include <face_pthread.h> to #include "face_pthread.h", but it doesn't solve this problem, so I hope someone can help me.

I add a case to show my current situation.

test.h

#ifndef __TEST__
#define __TEST__

typedef unsigned char INT8U;
typedef INT8U clockid_t;

void test();

#endif

test.c

#include "test.h"

void test() {
    clockid_t time;
    return;
}

I compile the code with arm-none-eabi-gcc

The following is the error message

.../Test/test.h:5:15: error: conflicting types for 'clockid_t'
    5 | typedef INT8U clockid_t;
      | 
arm-none-eabi\include\sys\types.h:199:21: note: previous declaration of 'clockid_t' was here
  199 | typedef __clockid_t clockid_t;
      |         

Wait a minute, when I added the error case, I found that the operating system I used introduced the header file #include <stdio.h> of the compilation toolchain in a certain configuration file, and introduced related files #include <sys/types.h> in stdio.h, it seems that this error is caused, maybe I need to change the question: is there any way to avoid this conflict?

AlgoOy
  • 11
  • 3
  • There's a number of similar questions here at SO. Here's a couple: https://stackoverflow.com/q/37639205/147407, https://stackoverflow.com/q/28227266/147407 – Andrejs Cainikovs Dec 16 '22 at 11:25
  • Does this answer your question? [If a standard library function is reimplemented, which of the two is called?](https://stackoverflow.com/questions/37639205/if-a-standard-library-function-is-reimplemented-which-of-the-two-is-called) – Andrejs Cainikovs Dec 16 '22 at 11:25
  • I guess you want to find a way to tell the compiler to not load the default pthread library? Does that one actually work? If it works, why are you writing your own one? If not, why is it part of the toolchain? – user253751 Dec 16 '22 at 11:32
  • @AndrejsCainikovs this question is not about standard library *functions*, but type definition. The problem is at compile time, not link time. The suggested duplicates don't really apply. – Marco Bonelli Dec 16 '22 at 13:13
  • You will need to add a [mre] of the code you are trying to compile that produces the error and also state which toolchain you are using and how you are compiling (command line options etc). – Marco Bonelli Dec 16 '22 at 13:14
  • @MarcoBonelli Right. Original question was about redefining posix functions, but now it went a bit other direction. – Andrejs Cainikovs Dec 16 '22 at 18:36

1 Answers1

0

NB: several possible duplicate questions relate to re-implementing standard library functions. This question is about re-implementing standard library types, and I think it is a legitimate fact-based question and I couldn't find a duplicate.

You are trying to re-implement part of the standard library which is included as part of your compiler package.

You can do this, but it is a lot more work than you perhaps think it is.

You can start by adding the command line argument -nostdinc to gcc. This will stop it being able to find the headers that contain the duplicates. It will also stop it from finding a great many other things that perhaps you wanted. You will have to re-implement all of them too.

If you want to avoid duplicate symbols when linking as well as duplicate type definitions when compiling then you will probably need -nostdlib. You may also need -ffreestanding. See the gcc manual and the questions linked in the comments for details.

Tom V
  • 4,827
  • 2
  • 5
  • 22