tay10r
tay10r

Reputation: 4357

Getting function names programmatically

I'm making a build that uses a lot of function pointers, with a struct that vaguely looks like this:

struct functionArray {
    void (*func)(void);
} funcarray[20] = {
    func1,
    func2,
    func3,
    func4
};

Where func1, func2, ... and func20 are in a file called functions.c

I want to be able to do this during the build. For example, if I used a macro instead:

funcarray[20] = {
    FUNCTION_LIST
};

I may be able to do this with a generated header file. Sort of like:

// testheader.h.in
#define FUNCTION_LIST @functions_in_file@

Is there anyway I can do this? I know I could just go back and forth editing files, but I this is the second or third time I've encourted this. I looked through the [cmake reference][1] and didn't see anything that struck out to me. Could I somehow using objdump or nm to pipe the function names to a cmake file?

Edit:

Getting the function names from the .o file should work, too. I guess there's no reason for it to be a .c file.

Upvotes: 1

Views: 150

Answers (1)

Ingo Blackman
Ingo Blackman

Reputation: 990

Ok, here is one way to do it. Generate the object file functions.o and then use

nm functions.o | awk '/[0-9a-f]+ T / { print $3 "," }' > function_list.h

With the code like this

struct functionArray {
    void (*func)(void);
} funcarray[20] = {
#include "function_list.h"
};

You should be able to then integrate that. Note though that the number of functions in "functions.o" needs to be exactly 20 here.

Upvotes: 2

Related Questions