Need Preprocessor macro

Hello,
who can help me. I need one preprocessor macro.

Input: Name
result: Modulename_Name

#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
Modulename =__FILENAME__ - extention

#define objModule(__NAME) __Modulename__##__NAME
Sample:

filename: "Test.cpp"

objModule(my) => Reult: my_Test

I hope anyone can do. Thanks
Last edited on
If it's C++, why does it need to be a #define?

https://en.cppreference.com/w/cpp/language/constexpr
What about an inline function?

What's your real question?
http://xyproblem.info/
Assuming there are valid reasons not to take @salem c's advice (although I can't think of any right now), I am confused about a few things.

1. Macros beginning with (i) 2 underscores or (ii) an underscore and an capital letter are reserved for the compiler. You should not be defining macros with those names.

2. Is ModuleName supposed to be a macro also? You have Modulename and __Modulename__ in your code. Those are 2 different tokens.

3. Modulename =__FILENAME__ - extention
Assuming you mean __Modulename__, are you asking how to remove the extension? If you don't mind corrupting the filename that you pass in, simply use strrchr to fine the "." for the extension and replace it with '\0'. Otherwise, you really should use a function to do this.

4. #define objModule(__NAME) __Modulename__##__NAME

objModule(my) => Reult: my_Test

Your objModule macro takes __NAME as an argument and concatenates it at the end of the string to which it evaluates. Your example code wants it to be at the beginning of the evaluated string. If you want __NAME at the begging of the final string, it needs to be at the beginning of the macro definition. Also, you haven't added the underscore to your resultant string.
Does the macro need to expand to a string literal or is it OK if it generates a std::string?
This is the basename() function in UNIX systems.

To avoid calling strrchr() twice, make it an inline function as salem c suggests.
1
2
3
4
inline const char *basename(const char *path) {
    const char *cp = strrchr(path, '/');
    return (cp ? cp+1 : path);
}
...and in C++17, you should be using filesystem::path for this kind of stuff.

But we are missing the point.
Whatever you are trying to do, it is probably wrong.

For what reason do you need the source filename at build time?
Perhaps we can suggest a better approach.
Topic archived. No new replies allowed.