Using both gcc with -std=c11 and g++ with -std=c++14.
E.g. for a file named src/dir/Hello.cxx
it should expand to something like e.g.:
const char basename[] = "Hello";
or
const char basename[] = getStaticBasename(__FILE__);
as where getStaticBasename()
is a macro (for C sources) or constexpr function (for C++ sources) which results to "Hello".
I have to avoid splitting the string from __FILE__
at runtime, because the path and suffix must not be compiled into the executable in any way.
The solution must be without dependencies to huge libraries such as boost.
As I have no makefiles, solutions like this cannot be used in my case.
Did one have a solution for that?
Edit 2015-07-02:
- I have no influence on how the compiler and linker was invoked (sometimes via makefile, sometimes from command line, or some IDE (Eclipse CDT managed make, Crossworks, Xcode et cetera. So the solution needs to be in code only.
- My use case is to provide some kind of "generic region identifier" for a small footprint logging solution. The application code (which uses my logger) should only
#include <Joe/Logger.h>
and within the later calls to e.g.LOG_DEBUG(...)
I'll implicitely take use of the automatically generated "generic region identifier". - My current solution is that the application code have to declare a
JOE_LOG_FILE_REGION(Hello);
(after#include <Joe/Logger.h>
) before it could placeLOG_DEBUG(...)
in its code.
It turns out to be very simple, you just need the
#line
preprocessor directive, exampleat the top of the file, this as is, if all you want is to hide the file name completely then
would work.
If you don't want to use
Makefile
s, you can use thisThe
-xc
gcc flag above means (from gcc's documentation):If you don't have any sort of script that helps you building the source then there is no way to do it I think.
Also, you can see from the above quote of the gcc documentation, that you can save the files without any extension at all, and then combine @Lundin's original solution with this and use
in this case
__FILE__
would expand to"filename_without_extension"
, and you would achieve what you want, although you need to compile the file in the same directory where it lives, because otherwise it will contain the path to the file.