What is the purpose of a makefile? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What does it do? Do you just run make on the command line? Is the makefile just like a list of commands to execute and at the end of the make command you have a bunch of executable files?

The answer above is pretty correct, but misses an important point: makefiles are intelligent, they are meant to run only the needed commands, not all. This is done with the concept of dependencies between items, like:
to generate A from B, it is necessary to run (for example) "cc -o A B".
These rules/dependencies can be cascaded (to have A you must use B; to have B, you must use C+D+E, to have D you must do ...)
If your project is structured in many files, a makefile will (normally) recreate the objects whose dependencies are changed, not all the objects.
Think of a C project split in 10 files. You modify one, say "main.c" and then want to test the project. With Makefile, only "main.c" gets recompiled and then the final executable gets linked. Without a Makefile, perhaps all the 10 files would get recompiled.

Yes it is essentially just a list of instructions. The purpose of a makefile is to be easily build an executable that might take many commands to create (which would be a pain to compile over and over again manually).

Related

Configure $(CC) to warn when inclusion is safe to be removed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Read this source code doc:
// foo.c
#include<stdint.h>
main(){}
I can do this:
$ gcc -Wno-implicit-int foo.c
$ ./a.out
$ gcc -dumpversion
6.3.0
GCC compiles without warnings.
Let's modify the source:
// foo.c
main(){}
But the same happens:
$ gcc -Wno-implicit-int foo.c
$ ./a.out
$ gcc -dumpversion
6.3.0
The output is the same. I want to believe that this means the inclusion can be removed safely.
Can I configure GCC in order to warn such inclusion can be safely removed?
What about the same for LLVM?
Is it costly for the compiler to figure out?
Would you ever consider activating the feature?
Expanding on Peter's comment, I'm going address the third question, regarding the cost of this. TL;DR: this is not functionality that would be trivial to add to a compiler.
Currently, the compiler simply processes the source, line by line, respecting #includes as a means to go and fetch a different source, and insert it at the appropriate place in the input stream. This is all handled by the preprocessor.
It goes as far as to add some special directives (typically #line), so that error messages match up with where they actually happen, but that's about it.
What would be needed to do what the OP is asking for is for every single declaration to have meta data added to it specifying which file it was found in. Then as the source is being processed, it would be necessary to mark every declaration that gets used. Then finally at the end of compilation, the compiler would be have to run over the entire symbol table, to see if any file has the condition that none of the symbols in it were ever used.
That's not a "five line of code" fix, it's going to be a fair sized investment.
And what I've just outlined doesn't begin to deal with nested #includes. Suppose outer.c includes middle.h. Now middle.h doesn't have any symbols in it that are used in outer.c but it does include inner.h that is used. So without saving the "route" to each variable, you risk throwing away middle.h and thus losing inner.h.

implicit dynamic linking vs explicit dynamic linking - which is more effective? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
There are two ways to link a shared library .
one named implicit dynamic linking and one named explicit dynamic linking.
I have googled some doc not found docs tells the difference on efficiency of the two .
Take a linux .so file as example . my doubt is : the implicit linking compare with the explicit way , will the explicit way cause more IO or cpu or memory somehow ?
Wondering which way is more effective and why ?
thanks a lot !
From what I understand, implicit dynamic linking is the fact of saying that your program needs the library in order to run, by adding the library in the dependency section of your program. If the library isn't found at the start of the program, the program simply won't be executed.
Explicit dynamic linking is using a function like "LoadLibrary" (windows) or "dlopen" (Linux) in order to load a library at runtime. It's exactly what a plugin is, and how you can code it.
Now, doing an explicit dynamic linking is going to add work and complexity, and I don't see any reason for it to be more efficient than an implicit dynamic linking. You use explicit dynamic linking only when you cannot do otherwise, like loading a library depending on some runtime value.

How do you add/specify predefined macros for GCC? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
important thing: -D does not apply here.
Is it possible to declare macros that appear in every compilation (much like predefined macros) in some dynamic manner (meaning I'm lazy to recompile gcc)? or do I have to recompile my gcc? Should I have to recompile, how do I specify my predefined macros?
You might consider providing some (or improving yours) spec file.
You could patch the gcc/c-family/c-cppbuiltin.c file of the source code of GCC.
You could code then use a GCC plugin defining additional predefined macros.
But I am sure it is a very bad idea; I recommend instead passing explicitly some -D flag to your compiler; your question smells badly as some XY problem. You need to motivate your question.
You could instead organize your PATH variable and add appropriately some gcc shell script adding that -DMACRO option and explicitly invoking e.g. /usr/bin/gcc with it.
On Linux you can
use an alias:
alias gcc="gcc -DMACRO1 -DMACRO2"
Copy old /usr/bin/gcc to /usr/bin/gcc.original. Make your own shell script and name it /usr/bin/gcc, inside which you have
exec /usr/bin/gcc.original -DMACRO1 -DMACRO2 "$#"

How can I write 'Hello world' in C without using any function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I found the way to do it on quora but I can not understand it.
You can achieve this using opcodes.....
const int main[] = { -443987883, 440, 113408, -1922629632, 4149, 899584, 84869120, 15544, 266023168, 1818576901, 1461743468, 1684828783, -1017312735};
Now follow below steps....
Save that in hello.c
then gcc -o hello hello.c
then ./hello
Hola..... Hello World!
Op-codes will be specific for specific hardware and controller based on architecture of it. If I understood correctly then op-codes will be different for different hardware and platforms. So this is not a generic solution. However I tried to compiler it on IDEone & it gives run time error. Is there any generic solution?
Similar question on SE
In C, the main function is the entrypoint into your program. In the program binary, the machine code into which the function is compile will get placed in the .text section of the binary as well as the location of the main function in the symbol table.
In the above code we create a constant array with the same main symbol name. Since the array is constant, this will also get placed in the .text section of the binary. And since any code in the .text section is executable, there is nothing to stop the opcodes in the main array getting executed.

Create a program from another program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm making an application that will create a working executable based on what the end-user inputs into the program.
For example:
if (make_annoying_sounds == true)
{
//Generates an executable that makes annoying beeping sounds
}
else
{
//Generates an executable that doesn't make annoying beeping sounds
}
Basically I want my program to generate/create another program. I've seen/used many programs that do this. I have searched all over the internet and can't find anything. All help is appreciated. (Create a program, from within my program).
Try using a basic system call to invoke a compiler after you've created the source file.
You can create the source file with just the utilities found in stdio.h
Security Note: The system function is known to be dangerous. When in doubt, call a function like exec to invoke the compiler. Although exec erases the currently running process, so you should use fork and then call exec if you want to keep doing stuff after the compilation has finished.
So you want to create a compiler? This question below contains a whole list of resources to help you get started.
Learning to write a compiler
You need to do the following:
Based on the user input, generate the code for the custom program.
Compile that code into an executable file.
Theorically, you could, depending on what the user inputs, make your C code generate C-code inside your if statements. However this would be quite difficult.
The best way I think is to make an independent C engine which will only implement functions that any of the generated program can execute (playing the sound given in parameter, for example). The program you are trying to code (not the engine, but the one with the if statements, let's call it the "master program") must generate code which implements the algorithm which will choose what function of the engine to call and when. This generated code should be written in a scripting language like lua, since in is easier to generate script code than C. Thus, the engine should be designed to be able to communicate with Lua scripts. When the user clicks on the final "generate program" button of the master program, the master program calls gcc to compile the engine and the Lua script to generate the program the user tried to create. This is long, but this is, I think, the right way to do it.

Resources