1

I have a question. I was wondering if you could re-compile code with another piece of code. For example (theoretical):

main.c:

#include <stdio.h>

void showme();

int main()
{
   showme();
}

void showme()
{
   fprintf(stderr, "errtest, show me");
}

Compile this file to main. (So the main is compiled) After this I want to add a piece of code.

addthis.c:

void test()
{
   test();
}

Now I want to use the (compiled) main and re-compile it with addthis.c. When running it (./mainWithAddthis) should show the print 2 times.

I hope I explained it clear. Anybody an idea?

Tim
  • 21
  • 2
  • 5
    You can compile a file without having to define all the functions that are called. It's only at link time that you need the functions to be defined. In order for an executable to be created you do need all the functions to be defined. Your question is rather odd. What's your real problem? – David Heffernan May 09 '11 at 20:02
  • 3
    Is this homework? If so, please add the `homework` tag to your question. – Merlyn Morgan-Graham May 09 '11 at 20:07
  • Just exploring the boundaries of C. – Tim May 09 '11 at 20:22
  • 1
    @Tim In your imagination, what did you think would happen when the first executable called the function that did not exist? Also, what's the deal with the infinite recursion in `test()`, and why do you bother with `test()` since nothing calls it (apart from itself)! – David Heffernan May 09 '11 at 20:28
  • Are you maybe thinking of creating a dynamic or shared library, such that you can load the definition of `test` at run time? – John Bode May 09 '11 at 20:45
  • 1
    Question makes no sense; `test` is never called... – R.. GitHub STOP HELPING ICE May 09 '11 at 20:57
  • 1
    @Tim: Separate compilation is hardly "the boundaries of C", it is the norm. Yes you can compile and link spearate modules, but your example code makes little sense in that context. – Clifford May 09 '11 at 22:06
  • "I hope I explained it clear." -- No, actually, you did the opposite. – Jim Balter May 10 '11 at 02:11

5 Answers5

6

You need a forward declaration for your void test() like you have one for the void showme(). Compile each .c file with -c (compile only) option:

  • gcc -c addthis.c -o addthis.o
  • gcc -c main.c -o main.o

Then link the two object files with:

  • gcc main.o addthis.o -o main

Then enjoy ./main :-)

Notinlist
  • 16,144
  • 10
  • 57
  • 99
  • 1
    That's not what was stated in the question. He wants to use the compiled binary of **main** with the object file of **addthis.o** to create a new binary. I don't think this can be done! – karlphillip May 09 '11 at 20:05
  • 1
    I agree with karl, this isn't what OP was asking for, but then again the question is very unclear. – David Heffernan May 09 '11 at 20:09
  • You assumed he wanted to use gcc, too, which he didn't specify. But this is good info, since it gives a concrete example and shows that building and linking are a two-step process. – Merlyn Morgan-Graham May 09 '11 at 20:16
  • @Merlyn: He said `./mainWithAddthis` so I said `gcc` :-) – Notinlist May 09 '11 at 20:19
  • After understanding the answers, this answer is objective, clear and simple. So there you go. – Tim May 09 '11 at 20:26
  • How odd that this completely wrong answer got upvotes and acceptance. A forward reference of `test()` is irrelevant and achieves nothing since `test()` is never called in main.c ... and if it were called, it would blow the stack, certainly not "show the print 2 times". – Jim Balter May 10 '11 at 02:15
  • "He said ./mainWithAddthis so I said gcc" -- that makes no sense at all. At the very least you could use that name in your answer. – Jim Balter May 10 '11 at 02:17
  • @JimBalter He asked something unclear. I was the best at guessing the intentions and the gap of knowledge behind. Was I speculative? Indeed. But finally he had what he wanted (not what he asked). Mind readers for the win! :-P :-) – Notinlist Sep 29 '11 at 16:00
2

Your first code will not compile since there's not definition of test();.

As I understand, you want to take the compiled main and add it with the code generated on addthis.o to create a 2nd application named mainWithAddthis. This is not possible!

You are either confused or trying to do some hardcore trick.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • If you can provide a valid example (compilable code) I'll be glad to answer your question. – karlphillip May 09 '11 at 20:02
  • Traditionally, if you want to add or replace a feature on your application, you have to recompile the sources. You can't use an existing binary + new code to build a new one from that. But, in case you have access to both sources you can do: `g++ main.o addthis.o -o main` – karlphillip May 09 '11 at 20:16
  • "Your first code will not compile since there's not definition of test();" -- that makes no sense; the only call of `test()` is in addthis.c, inside the definition of `test()` -- which compiles just fine. – Jim Balter May 10 '11 at 02:19
  • @Jim The code was changed since that statement. There's no EDIT box displayed in the question because the user did it within the firsts 2 min (or something like that) and didn't wrote anything in the *Edit Summary* box. – karlphillip May 10 '11 at 02:35
  • @karlphillip Good lord, really? Another idiotic time-based SO "feature" .... but you comment above says that your answer is "updated" -- it should refer to the current question, not something forever hidden. – Jim Balter May 10 '11 at 02:39
0

Building an executable is a two step process.

  1. For every source file you specify (in your project/makefile), your compiler will build an object file
  2. For every object file you specify (in your project/makefile), your linker will link them together and make your executable

One way to re-compile would be simply to re-build your entire project. You'd get more or less the same result.

But it sounds like what you want to do is recompile only the source file, addthis.c, then re-link the old version of main.o (the object file compiled for main.c) with the new version of addthis.o. How to do this is completely dependent on the compiler and build system you use.

Also, that solution will only work if you have main.o, addthis.c, and have the exact same compiler binaries/install, and compiler flags used to generate main.o. If this is all on your box, then you're probably okay.

If you only have the files addthis.c and main.exe, then no there is no portable way to do what you want.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
0

You can't do what you are talking about after the fact without some hardcore time with a hex editor.

However, if you plan ahead and build it into your software, you can use dynamic loading to achieve the same effect, which is how a lot of software provides plugin functionality. Check out glib modules for a common way to do this in C.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
0

main.c

void f();
int main()
{
  f();
  return 0;
}

addon1.c

#include <stdio.h>
void f()
{
  printf("I am the ONE.\n");
}

addon2.c

#include <stdio.h>
void f()
{
  printf("I am the TWO.\n");
}

Compilation

gcc -c main.c -o main.o
gcc -c addon1.c -o addon1.o
gcc -c addon2.c -o addon2.o
gcc main.o addon1.o -o main1
gcc main.o addon2.o -o main2

You will have ./main1 and ./main2 programs which will print ...ONE. and ...TWO..

Notinlist
  • 16,144
  • 10
  • 57
  • 99