is there a trick to using inline functions?

Hi -

I've looked at the doc, and can't see what I'm doing wrong. I have a function that I want to make inline, so I've put "inline" in front of both the definition and the declaration.

1
2
3
4
5
6
#ifndef TRIM_H
#define TRIM_H

inline int32_t TRIM (int32_t i, int32_t n);

#endif // TRIM_H 


1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>
#include <tr1/cstdint>

inline int32_t TRIM (int32_t i, int32_t n)
{
    int32_t     temp;
    if (n <= 0) // n must be a positive number.
        exit (1);

    temp = i % (1 << n);
    return temp;
}


The linker is objecting:

Undefined symbols:
"TRIM(int, int)"


What am I doing wrong? Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TRIM_H
#define TRIM_H

inline int32_t TRIM (int32_t i, int32_t n)
{
    int32_t     temp;
    if (n <= 0) // n must be a positive number.
        exit (1);

    temp = i % (1 << n);
    return temp;
}
#endif // TRIM_H  


inline function definitions should be included in the header, otherwise the compiler can't see the definition to inline it outside of the source file it was defined in.

Btw, if n must be a positive number, why use a signed variable?
Last edited on
Are you including that header file in some other source, perhaps? inline functions are (usually) defined entirely in the header files.
Thanks, guys. I should have been able to figure out that one for myself.

cire: this function is being used in a circuit emulation program. In the circuit, there are data paths of varying widths. The variable n is used to tell the function how many bits wide to trim a 32-bit int. This applies to positive and negative numbers.
Topic archived. No new replies allowed.