I am looking for a way to find the name of the executable file which runs it. meaning that if I have a file called program which runs something I would like to get its name.
Using __FILE__ does not suite me since I need the executable name not the C files name which contains the code.
I am wondering if there is a gcc macro which can help me or a built in function which I couldn't find.
EDIT:
Also using argv[0] is not appropriate since I need to call the function not only inside main.
And passing the argv[0] parameter to all the functions that might need this parameter also isnt acceptable since it will be used in the entire system (I need to hash by it).
int main(int argc,char* argv[])
{
printf("%s",argv[0]);
return 0;
}
The first argument(argv[0]) contains the name of the program which is being run.
The standard definition for main() in C is:
int main(int argc, char **argv)
argv[0] contains the name of the program as executed.
From main, pass argv[0] to wherever you might need the program's name.
Or if a lot of functions need it and you want to avoid passing it around too much, assign it to a global variable.
You asked for macros as a solution, but they are expanded at compile time. And since the name of the executable file can be changed after compilation, what you want cannot be determined at compile time, so macros are out of question.
Often remembering argv[0] from main will be quite sufficient.
If this is not the case -- for example if you're a shared library, or if you worry that your caller started you with a faked argv[0] (this is possible but unusual), you can, on POSIX-compliant systems with a mounted /proc, use readlink to resolve /proc/self/exe and get a path to the main binary of the running process:
#include <limits.h>
#include <unistd.h>
// Note that readlink does not null-terminate the result itself. This
// is important if the link target contains null bytes itself, I suppose,
// but it means that you have to take care that the null-terminator is
// placed yourself if you're going to use the file name as a string.
char buf[PATH_MAX] = { 0 };
readlink("/proc/self/exe", buf, PATH_MAX);
And on Windows, there is the GetModuleFileName function:
#include <windows.h>
TCHAR buf[MAX_PATH];
GetModuleFileName(NULL, buf, MAX_PATH);
The startup name is given via main()'s argv[0] and can be considered constant.
So why not make it available through out the program using a global reference to it.
progname.h
#ifndef PROGNAME_H
#define PROGNAME_H
extern char * progname;
#endif
main.c
#include "progname.h"
char * progname = NULL;
int main(int argc, char ** argv)
{
progname = argv[0];
...
}
Access it from any where like so
module1.c
#include <stdio.h>
#include "progname.h"
void f(void)
{
printf("progname='%s'\n", progname ?progname :"<not set>");
}
Related
I need to run many times a code of mine, changing a variable defined in an header file.
I wanted to write a bash script that compiles (through makefile) and runs
./myprogram some_value
is there a way to use what I recover from atof(argv[1]) inside an header I'm including inside myprogram.c ? (An header shared between many other files, containing the values for the parameters I'm using in my code)
In that header I'd have something like
float the_parameter;
that I want to initialize to atof(argv[1])
Firstly, put a declaration of the variable with extern in your header file.
extern float the_parameter;
Then, in one of your source file, declare the variable without extern and initialize that.
#include <stdlib.h>
float the_parameter;
int main(int argc, char* argv[]) {
if (argc > 1) the_parameter = atof(argv[1]);
/* other code */
}
I'm tasked with creating a popen() function using Pipes, which I have already completed. The catch is that the function must ALSO be named popen, not mypopen or popen2 etc.
I'm looking for someone to guide me in the right direction as to how I can accomplish this.
Currently I have a header file popen.h and another filed popen.c that I have created.
popen.h just has
void popen(char *arr[]);
While popen.c has the actual popen function code (Which works, just not when the function name is popen).
Currently I'm getting a previous declaration error upon compilation when my function is called popen.
How can I let the compiler choose between the two different popen functions, mine and stdio.h version.
Just #define popen my_popen before declaring, defining and using your function, but after including <stdio.h>.
popen.h
#define popen my_popen
void popen(char *arr[]);
popen.c
#include <stdio.h>
#include "popen.h"
void popen(char *arr[]){ printf("that's NOT the stdio's popen\n"); }
main.c
#include <stdio.h>
#include "popen.h"
int main(void){
popen(0); printf("done\n");
}
Similarly, you can hide the stdio's popen by defining popen to stdio_popen before including <stdio.h> and undefining it afterwards, if you really want to have a symbol named popen:
#define popen stdio_popen
#include <stdio.h>
#undef popen
void popen(char *arr[]){ printf("that's NOT the stdio's popen\n"); }
int main(void){
popen(0); printf("done\n");
}
$ nm a.out | grep popen
0000000000001160 T popen
Such nasty tricks are only useful when having to combine ornery source codes which pollute each other's namespace. There's zero reason to name a function you just wrote popen() and not something else, unless it's a drop-in replacement, with the same interface as the standard one.
If you want to have popen safely you must also make sure that you aren’t using <stdio.h>as this is where the normal popen is located, and your linker will not like it if there are two identical symbols named popen.
I am learning C for an operating systems course and have just finished writing this program as per the textbook instructions:
#include <dirent.h>
#include <stdio.h>
#include "quit.h"
int main(int argc, char **argv) {
DIR *dir;
struct dirent *direntry;
arg_check(2, argc, "Specify a directory\n", 1);
if ( (dir = opendir(argv[1])) == NULL)
quit("opendir", 1);
while ((direntry = readdir(dir)) != NULL)
printf("%10d %s\n", direntry->d_ino, direntry->d_name);
closedir(dir);
exit(0);
}
This code is exactly copied from the textbook, but quit.h appears to be causing the compile to file. I have tried switching "quit.h" to , and "quit", but none of these have worked, and I cannot find other questions about this specific issue.
include "quit.h"
The word #include (with its hash # prefix) means that a file has to be read in; the content of that file (quit.h in this case) is processed exactly as if it was typed inside the program. Suppose you have a file name "test.h" which contains the single line
// this is a test
if you a have a program like this:
#include "test.h"
int main(int argc, char **argv)
...
the compiler processes (sees) exactly these lines:
// this is a test
int main(int argc, char **argv)
...
The file name specified after #include can be enclosed with angles or quotes. Conventionally, if angles are used, like
#include <stdio.h>
this means that the file (stdio.h in this case) is some system or standard file or so - in other simple words, someone else has written that file for you. If quotes are used, instead, the file specified is considered somehow part of the program you are compiling. Your quit.h seems like this. So you must have a file named quit.h. If you change that "quit.h" to "quit", a file named quit must be present in the same directory of the file you are compiling. (Actually things are more complicated, but don't mind it for now). Read your book, somewhere it should explain what is that file "quit.h".
I want to be able to modify a variable from one C file and see the changes of that variable on the other one
Here are my C files:
file1.c:
#include "myheader.h"
int
main( void )
{
printf("Variable %d\n", var);
}
file2.c:
#include "myheader.h"
int main(void){
int var = 1;
var = var + 1;
}
The header file looks like this:
extern int var;
Now, I want to run file2.c first and than when I run file1.c print the incremented value of var. Ideas?
I think I am doing everything like this answer, but can't make it work.
PS: Just started learning C.
Each of your "files".c has a main function and thus will compile into a separate executable.
So you can't have one executable increment a variable in its own address space and then have another executable see the change in someone else's address space - the other executable will have an own variable in its own address space. Unless you would use advanced inter process communication (but that is Lesson 42).
I have a lot of unused macros in my code.
So, I am wondering.. If a macro is unused, does it takes up memory space in your program?
The type of macros I have are just the basic ones.
Example:
#define TEST_ID 0
Macros will be expanded during preprocessing phase so they don't exist in your program. They just take some space in your source code.
Edit:
In response to Barmar's comment, I did some research.
MSVC 2012: In debug build (when all optimizations are disabled, /Od), adding lines of macros won't cause the growth of the size of your program.
GCC: does provide a way to include macro in debugging information as long as you compile your program with a specific flag. See here. (I didn't know that before myself. Thank you, #Barmar, #Sydius)
No, doesn't takes space until is used, for this two pieces of code:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d %s\n", argc, argv[0]);
return 0;
}
and
#include <stdio.h>
#define TEST_ID 0
int main(int argc, char *argv[])
{
printf("%d %s\n", argc, argv[0]);
return 0;
}
The ASM generated with gcc -S is the same.
macro is replaced by preprocessor before compilng start.
if you define a macro, and doesn't use it, the compiler will never see it.