How to Compile a C program in C? - c

I am making a program in c that can produce another c code.
How to, using the first program, compile and run the second program immediately after the second program has been produced?

One way is to used system() call ...
system("cl generated_file.c -o gen_exe") ;
system("./gen.exe");
or
system("gcc generated_file.c -o gen.exe");
system("./gen.exe");
Or you use a simle batch or script or makefile to do this

I'll make two notes in one:
First, if you have one program that generates source code, why not use the normal build system to handle this for you? For a Make-based build system it could look something like this:
second_program : second.c
$(CC) $(CFLAGS) -o $# $<
second.c : first_program
./first_program $(GENERATION_OPTIONS) > $#
first_program : $(LIST_OF_SOURCE_FILES)
$(CC) $(CFLAGS) -o $# $<
This would be more in line with the Unix philosophy than having the first program run an external command, which is always nice.
Second, do you want the second program to be generated and executed dynamically? I.e. will the resulting code depend on some dynamic state of the first program, and could the output from the second program be relevant to the first?
If so, perhaps you should take a look at what you can do with a library to run some script language like LUA or ECMAScript.
(This is perhaps a bit too advanced for the case you are asking about, but it's always nice to know what options there are.)

You'd need to embed a compiler into your app(such as libtcc or lcc) or invoke one via a command line, but that requires detecting what the user has installed or at the very least including extra binaries with your app

Related

How exactly "make all" works?

So I am studying makefiles usage. Let's say I 've many functions fun_i.c where i is in [1,100]. I have wrote them all in seperate .c files in order to make faster compilation when I only change some of them. I need to create a Makefile. I was thinking something like that:
all : program
program: fun1.o fun2.o ........ fun100.o main.o
gcc -o program fun1.o fun2.o ..... fun100.o
fun1.o: fun1.c
gcc -c fun1.c
.
.
.
fun100.o: fun100.c
gcc -c fun100.c
main.o : main.c
gcc -c main.c
I 've read many articles online talking about how the target "all" makes sure that everytime I change something in a function and I call make it will update only what changes (so I'm going to avoid uneccessary recompilation).
However, I am not sure if I am using it right here. I checked my makefile ( with 2 functions though) and it worked fine. But I am not sure if that's the way to take advantage of all here.
I tried to remove it and just leave program and below.. there and it worked as fine.
What am I missing?
Could you may provide examples that all is critical?
I 've read many articles online talking about how the target "all" makes sure that everytime I change something in a function and I call make it will update only what changes
all does not mean anything. It's just a convention. You have to define the targets correctly to do what you want. If you want make all to recompile everything, you need to define it so that it does that.
so I'm going to avoid uneccessary recompilation
In practice, I'd say that this is a completely unnecessary concern, and it's a waste of time creating makefiles that does this. If you have a large project you should not create makefiles manually. You use a build system like cmake.
If the project is small and only contain a few files, then the compilation time is in general not an issue. For a small project, my goal with a makefile would be to not have to type the compiler flags all the time.
Here is a makefile that would work fine for most very small projects like school assignments:
CFLAGS=-Wall -Wextra -pedantic -std=c17 -fsanitize=address
LIBS=-lm -lpthread
program:
gcc $(CFLAGS) $(LIBS) *.c
clean:
rm -rf *.o *~
Your makefile is fine, both with and without all: program.
The word all has no special significance in makefiles, using is just a convention. In your makefile all has no recipe (i.e. no commands associated with it), so make all and make program are equivalent.
Note that if you don't specify a target when invoking Make (e.g. make as opposed to make program), the first target in the makefile is built.

Can a C program be executed, in the same makefile as it is being built?

I have a makefile which builds my .C project, and produces an executable file. I know this can then be run from using the command ./(NAME) from the terminal.
My question is this - is it possible to actually build AND run within the makefile, so that i would just be able to type 'make' in the terminal, and the program would be built and run, all from this one file & command? (without the need of manually running the executable file).
Thanks :)
No idea what is you makefile, but you could do something like that:
PROGARGS=
.PHONY:all run
all:prog
#...
run:all
./prog $(PROGARGS)
Yes, it can.
For example:
TARGET=hoge
CC=gcc
.PHONY: compileandrun
compileandrun: $(TARGET)
./$(TARGET)
$(TARGET): $(TARGET).c
$(CC) -o $(TARGET) $(TARGET).c
If you have this Makefile and the source code hoge.c in your writable current directory, the program will be compiled and run with make command.
The other answers are good; however, you can use this other variant (a quick-and-dirty solution).
Suppose your makefile has this line to build your program:
NAME:
gcc source.c -o NAME
You can add your running command directly below:
NAME:
gcc source.c -o NAME
./NAME
This does the same as && concatenation: it runs the first command, and if successful, the second command too.
As long as you sequence your makefile dependencies correctly, there is nothing stopping you from running you C program from the makefile. That said, most people would expect a makefile to just build the file and not also run it at the same time.
I would recommend just writing the make command and the run command in a single line in the command line. This way you only need to press enter once but don't need to do anything fancy in the makefile itself. If your shell supports history completion, the next time you want to build and run again you only need to press "UP" and "ENTER"...
make && ./NAME
If you really want to put everything inside the makefile, I would recommend creating a separate "run" target in your makefile to reduce the chance of confusion, like in jdarthenay's answer.

How to produce a makefile that runs multiple programs after each other in c?

I have written two programs. The first one, ProgramOne, creates inputfiles for the second program, ProgramTwo (The two programs have no files in common, the output written by ProgramOne is just the input of ProgramTwo). Sometimes running the first program takes some time, but on other times it takes just a few seconds, depending on the dimensions I am simulating at.
I would therefore like to put all my files into one folder and create a makefile that is able to make commands to run:
ProgramOne
ProgramTwo
The two programs right after each other
The first two are not that difficult to write, the third one is the problem however (So at the moment I am able to run ProgramOne, then run ProgramTwo, which takes me two commands in the terminal, but I would like to make this one command as well in some cases). How do I make clear to the compiler that I want the two programs executed after each other.
At the moment I have a fairly standard makefile, but I have written the following:
(note that programone.c contains a main function, and only a main-function, depending on nothing from SRCS (the files for ProgramTwo))
(Another important note: to run ProgramTwo, I need to specify some arguments. In the terminal I would write: ./RunProgramTwo -v results. How do I need to specify these arguments whenever I would like to run the two programs after each other?)
(I also left some things out, like a clean and distclean command, but I do not think they are relevant to this case. If you think so however, I will provide my complete makefile upon request)
NVCC = nvcc
NVCFLAGS = -arch=sm_30
CFLAGS = -Wall -pedantic -g
LDLIBS = -lz -lpthread
SRCS := allfiles.c
OBJS := allfiles.o
all: .depend RunProgramOne RunProgramTwo RunAll
RunProgramOne: $ programone.o
${NVCC} ${NVCFLAGS} ${LDLIBS} $^ -o $#
RunProgramTwo: ${OBJS} main.o
${NVCC} ${NVCFLAGS} ${LDLIBS} $^ -o $#
RunAll: RunProgramOne RunProgramTwo
depend:
${NVCC} -M ${SRCS} > .depend
.depend: ${SRCS} makefile
${NVCC} -M ${SRCS} > .depend
This does not seem to do the job as I just get ProgramTwo running when I invoke ./RunAll.
Note that I am currently thinking of a solution where my two programs are decoupled from each other. I am also able to make the main-function in ProgramOne into a function, which I then invoke at the very beginning of the main-function of ProgramTwo. In this way, I can easily generate a makefile command for the third of my options, namely the RunAll, but then I do not know how I can achieve the first two.
I would prefer a solution such that my two programs are decoupled, but if this is not possible, feel free to tell me any possible way I can make this work.
I already looked up basic information about makefiles (simple tutorials) and looked at the manual for makefile itself, but I do not seem to find a solution.
Any help is greatly appreciated.
This target doesn't run anything: it just declares prerequisites but you have no recipe (the recipe is the thing that's passed to the shell and executed):
RunAll: RunProgramOne RunProgramTwo
If you want this target to run the programs you have to write a recipe that does this:
RunAll: RunProgramOne RunProgramTwo
./RunProgramOne
./RunProgramTwo

multiple small programs in one code::blocks project

I am new to Code::Blocks. For my studies I am programming several small programms in C (just to try things out).
I tried to put them all in one project, because they belong to the same topic, but that doesn't work because each of them has a main function.
Any ideas how I can put those files together somehow, but not depending on each other?
Suppose your source files are called
prog1.c
prog2.c
mylib.c
mylib.h
where prog1.c and prog2.c each contain a main() function and mylib.c (with the corresponding header file mylib.h) contains some library functions that should be linked to each program. In your Code::Blocks project you need to create multiple targets now. From the menu choose 'File/New/Build target...' and name them 'prog1' and 'prog2'. Now add all of your source files to the project (in case you haven't done so already).
Now right-click on prog1.c and select "Properties..." from the context menu. In the dialog box choose the 'Build' tab and make sure that under "Belongs to target" only "prog1" is checked. Do the same with prog2.c and the target "prog2". For "mylib.c" make sure that both "prog1" and "prog2" are checked.
Now you can easily choose in the UI which build target to build and run. Note that if you add another target, say "prog3", then you have to go to the build properties of prog1.c and prog2.c again and uncheck "prog3".
A C program should contain only one main function.. Divide all your separate programs as Functions and put them in a single C program or you can even put it in multiple files and compile them..
You can use a switch case for calling different functions..
Remove the main function from all the programs and convert them into functions..
Call them where ever required.. This will help you..
let's say that your project contains 3 c files and each c file is a separate program and it contains its own main function:
program1.c
program2.c
program3.c
So you can write a Makefile (its name in your computer should be "Makfile"). It will build the program you want separately
This is how your makefile looks:
all: program1 program2 program3
%.o: %.c
$(CC) $(CFLAGS) -c -o $# $^
program1: program1.o
$(CC) $(LDFLAGS) -o $# $^
program2: program2.o
$(CC) $(LDFLAGS) -o $# $^
program3: program3.o
$(CC) $(LDFLAGS) -o $# $^
clean:
rm -f *.o program1 program2 program3
with the above Makefile you can build each program separetly
example
$ make program1
will build only program1
or you can buil all programs with
$make all
It seems to me as if you are starting to build some nice utility functions. So follow the advice offered by raghu-srikanth-reddyce and create separate functions for each little program. However I would add that it would be better to create yourself a simple C library to keep them all in which you can link to at any time. Most professional programmers keep such libraries.
A simple makefile will enable you to build a final binary that you can link to.
Good luck with your programming ;)
If you want to compile in one file, you dont need a Project File then.
Just make a New File, then write all the code in a single file. Also use Function and Procedure if you need it.
This is quite late but since I was once here with the same question, and I believe most students had/ have/ will have this same question, let me elaborate where you have been played at.
In Educational Courses and in Real World, a project is (in general) some problem you work on, find a solution, then make a report (+ documentation) on it.
In Programming IDE, a project is the problem's solution itself. Hence, while you may have multiple individual problems within one project in any Python IDE, Any standard C/C++ IDE project must have only one 'main()' to rule them all (unless you know how to create makefiles).
What the AP tried to do is to put several different problem's solutions fit within one solution's space - not a possible thing to do in Code::Blocks. Even if all the problems may share the same topic (say, Graph Theory), they are individual problem (Dijkstra vs Floyd) themselves.

compile C program in multiple folders

I have a bunch of directories with C programs. I need to compile them one by one and use the result in the main Program.
So my Main program traverse in the Directory structure [I am not sure what the structure is, It may change over time] and compiles one C program at a time, use that result in some computation.
So If I write the main program in C and use nftw to traverse.
OR
Write a Shell main program.
Which appraoch is better?
I guess you use a compatible Unix/Linux/Cygwin system....
Therefore, I would advice to use a shell solution because it is more suitable for directory processing.
Makefile for each program
As #Dogbert said make can be used to build several programs. Build can be performed in parallel (using the option -j). Moreover make can also take care about the dependencies.
I do not know if you are used with Makefile syntax. Therefore I give a quick example about a program requiring three C files and one header file:
program: file1.o file2.o file3.o
gcc -o -o $# $^
%.o: %.c header.h
gcc -c -o $# $<
The following command will compile file1.c, file2.c and file3.c in parallel. Then link stage will wait for the completion of these tree compilations.
make -j3
Directory discovery
The following command finds each Makefile and runs the make command:
find . -name Makefile -exec make -j3 -f '{}' ';' 2>&1 | tee result.txt
If you have 8 cores in your computer, you can use -j8.
Reuse in your main program
The build result is displayed on the shell screen and is also stored in the result.txt file.
More information?
I do not know what is your system, or your knowledge. Hope this can help. If you are not sure to understand some parts, please ask for more information. ;-)

Resources