1

My project has the following 4 files: main.c, rcm.h, rcm.c and queue.c.

In rcm.h I'm declaring all functions implemented in rcm.c and queue.c.

rcm.c looks like:

#include "rcm.h"

void rcm (void) {

  Queue *Q = init(10);

  /* Some other stuff */
}

queue.c` looks like:

#include "rcm.h"

extern inline Queue* init(int n) {
  return malloc(sizeof(Queue*);
}

and rcm.h:

#ifndef __RCM__
#define __RCM__

typedef struct queue { /*...*/ } Queue;

void rcm( void );

inline Queue* init( int n );
#endif

When compiling I get the the warnings

 gcc-7 -O0 -c rcm.c -o rcm.o
 In file included from rcm.c:15:0:
 rcm.h:58:15: warning: inline function 'init' declared but never defined
 inline Queue* init(int size);
               ^~~~
 gcc-7    -c queue.c -o queue.o
 gcc-7 main.c lib/rcm.o queue.o -o main
In file included from main.c:4:0:
inc/rcm.h:58:15: warning: inline function 'init' declared but never defined
 inline Queue* init(int size);
           ^~~~

But, when I am not declaring init() as inline compiles normally.

2
  • The question is: Why do you declare the inline function init() in translation units, that do not define it? A function can only be inlined, if the definition is present in the source, it is not possible to inline it from other translation units. Commented Feb 3, 2020 at 12:23
  • 1
    Declare your inline functions (i.e. including the code) in the .h file. Commented Feb 3, 2020 at 13:08

1 Answer 1

1

inline Queue* init( int n );

In order for a compiler to be able to inline a function, it must know he code of a function. Without that knowledge, the compiler must emit a call to that function1. Hence the warning. In order to use an inline function in several modules, you can define it in the header as:

static inline Queue* init (int n)
{
    /* code here */
}

Cf. for example the GCC documentation for inline.

The reason for the warning is that you want the function to be inline, but you are hiding the code from the compiler: main.c includes the header that declares an inline function but in that compilation unit, init is defined (implemented) nowhere. 1 Except for functions built-in the compiler. In that case, you don't have to provide the code yourself, it compiler has build-in knowledge about it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.