开发者

Can I re-compile a file with new code?

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()
{
   tes开发者_如何学运维t();
}

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?


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 :-)


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.


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.


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.


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜