0

I'm working on a C program, it's a custom shell that asks the user to set different options (IP, port, ...) and then allow the user to build a binary based on these informations. I was wondering what is the best way to build a new binary from within an existing program ?

EDIT: My question may have been a bit unclear. The new compiled binary is a simple TCP client that will connect to the specified IP and Port. I don't want this new binary to be dependant of a config file. How can i build this from my existing C program ? Should i write the IP and Port to a .c file and then compile it using system("/bin/gcc ...") ?

13
  • 2
    What binary? For what? Binary is the most general format for any data on the computer. Commented Dec 9, 2019 at 16:02
  • 2
    Unless you have really strange constraints, just build one executable that reads a config file. You can generate that config file however you want. Commented Dec 9, 2019 at 16:04
  • 1
    What is this "binary" and what does it do? Is it accetable to require GCC, Visual Studio, or similar to be pre-installed on the system, or do you need a self-contained solution? Commented Dec 9, 2019 at 16:04
  • By binary i meant a new C compiled program with gcc. The binary is basically just a simple TCP client that will connect to the specified IP and Port Commented Dec 9, 2019 at 16:04
  • 2
    Are command line arguments out of the question? That depends on who the customer is for your program — if it's you, you'd probably be better served with a program that takes arguments (or a config file). If they're naïve end-users who can't be trusted to type the command name correctly, let alone remember arguments like port numbers and host names, then a shell script that fixes those parameters and invokes a program that expects arguments can work well. Or you can do as you propose. But recompiling a program to change simple values is not usually a good idea. Commented Dec 9, 2019 at 16:20

2 Answers 2

1

I think you are describing code generation and programmatically compiling a new executable. There are many ways to do what you have described. Here is a very simple (and very rough draft of a) set of steps to code gen, and compile:

1) Use printf, fgets calls from existing program to prompt user for a specific set of input values
2) Convert command line numeric input values if necessary. (Using atoi(), or strtod() for example)
3) Open a file for write, (eg FILE *fp = fopen(filespec, "w");)
4) Using fputs(), send a series of lines to the file comprised of a C source file, including values from steps 1&2 from the user input.
eg. fputs("#include _some file_", fp); (and other header files as needed)
eg, fputs("int main(int argc, char *argv[]) {", fp);
fputs(...) (all of the rest of your lines to make up the complete user defined code.)
fputs("return 0; }", fp);
5) close the file: fclose(fp);
6) Construct a gcc command line compile string to be used on command line on the file you just created.
7) Using popen() (If available) or system() or if using Windows, something like this to send a command to the OS, to execute gcc (or other compiler) on the command line to create your executable.

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what i asked ! However can i use a pre-written C source file and just add some data (here IP and Port) to it ? Like some kind of search and replace in C ?
Yes, kind of. If you have obtained user inputs from a very specific set of questions, as @Jonathan Leffler alluded to in his comments, and they are included in the text strings you prepare in your code generation steps, then those values should be sufficient to define certain programmed in behaviors for your new executable. Keep in mind, this is a very simple set of steps for the purpose of illustrating what it might look like to do what you have described.
1

If you have ready code in C all the options (like ip or other parameters write to the another file for example userdata.c

 #include "userdata.h" unsigned char IP[4] = {212,34,56,78}; const char IPstring[] = "212.34.56.78"; const char DNSIPstring[] = "4.4.4.4"; const char defaultGatewayIPstring[] = "212.34.56.1"; /* etc etc */ 

and userdata.h

#ifndef USERTATA_H #define USERTATA_H extern unsigned char IP[]; extern const char IPstring[]; extern const char DNSIPstring[]; extern const char defaultGatewayIPstring[]; /* etc etc */ #endif 

In your main program files just include the .h file. Do not forget to compile and link the .c file as well :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.