0
__FILE__ and __LINE__ for wide characters
Hello Trying to write the issue happening at runtime... so wanna use __LINE__ and __FILE__ as suggested by community members (sorry i forgot your names) in one of my question thread. __LINE__ gives int and hence not concerned much about this. My project is suppose to run on didferent machine having different languages... Is it good to use __FILE__ as it returns string not wide chars ? I am having assurity that application is going to be used in chinese and japanese language as well. Also do i need to use them in debug configuration only or it is fine to add these two macros in release configuration as well ?
9 Answers
+ 3
Isn't the filename of your file in your hands though? I don't see a situation where __FILE__ macro would have to deal with non-ascii characters unless you name your C++ file that way. And if do have non-ascii characters in your filename, then you can store it as a static wide string in the file.
+ 3
/*__FILE__ macro gives the path that was passed to the compiler for compiling. For example
g++ file.cpp
__FILE__ -> "file.cpp"
g++ ../cpp/file.cpp
__FILE__ -> "../cpp/file.cpp"
I searched but I can't find a way to get just the filename in any compiler. The most straightforward solution if you know the filename beforehand is to just store it either as a macro or in a static variable. Or else, you can make a constexpr (consteval to be specific) function that extracts the filename at compile-time.
I don't think there should be any problem in using these macros in release mode. But you should probably prefer std::source_location over macros once it becomes fully supported on all compilers
https://en.cppreference.com/w/cpp/utility/source_location
This thread has a lot of solutions for extracting the filename from __FILE__
https://stackoverflow.com/questions/8487986/file-macro-shows-full-path
Here is an example on how to do it at compile-time (I've used std::source_location but __FILE__ will work fine)
+ 3
[Continued]
https://code.sololearn.com/c8lNI0aHarsJ/?ref=app
+ 1
Sounds good... Filename should be in my control.... But full path is something making me confused.
How actually this works on deployed system ? Is it still give me full file path I have in development machine ?
Also can I use these two macro in release mode as well ?
+ 1
Thank you
0
Ketan Lalcheta are you trying to find the path to the source file, or to the executable?
0
Source file
- 2
That isn't a spam,, you can pub it using xml or html project
- 3
Its xml or html 20+ more