compile.c 1017 B

1234567891011121314151617181920212223242526272829303132
  1. #include "tools.h"
  2. #include <ccan/talloc/talloc.h>
  3. #include <stdlib.h>
  4. /* Compile multiple object files into a single. Returns errmsg if fails. */
  5. char *link_objects(const void *ctx, const char *outfile, const char *objs)
  6. {
  7. return run_command(ctx, "cc " CFLAGS " -c -o %s %s", outfile, objs);
  8. }
  9. /* Compile a single C file to an object file. Returns errmsg if fails. */
  10. char *compile_object(const void *ctx, const char *outfile, const char *cfile)
  11. {
  12. return run_command(ctx, "cc " CFLAGS " -c -o %s %s", outfile, cfile);
  13. }
  14. /* Compile and link single C file, with object files.
  15. * Returns name of result, or NULL (and fills in errmsg). */
  16. char *compile_and_link(const void *ctx, const char *cfile, const char *objs,
  17. const char *extra_cflags, const char *libs,
  18. char **errmsg)
  19. {
  20. char *file = temp_file(ctx, "");
  21. *errmsg = run_command(ctx, "cc " CFLAGS " %s -o %s %s %s %s",
  22. extra_cflags, file, cfile, objs, libs);
  23. if (*errmsg) {
  24. talloc_free(file);
  25. return NULL;
  26. }
  27. return file;
  28. }