2

Alongside main.c file, I have following my_custom_data_structure.c file in my project. My my_custom_data_structure.c file contains a lot of variables, functions, etc.

I am using #include "my_custom_data_structure.c" directive in main.c.

Problem

I would like to import only single function called foo from my_custom_data_structure.c. I don't need all the variables and functions, which are declared in my_custom_data_structure.c file.

Any insights appreciated.

File structure

-
|- main.c
|- my_custom_data_structure.c

Content of my_custom_data_structure.c

#include <stdio.h>


int DELAY = 20;

int SPEED = 7;

char GRANULARITY_CHAR = 'g';

unsigned int RANGE = 3;


void foo(){
    // TODO: In future, this function will print SPEED.
    printf("foo works!");
}

/*
    The rest of this file is filled by a lot 
    of code, which is not needed for main.c
*/

Content of main.c

#include <stdio.h>
#include "my_custom_data_structure.c"
    
int DELAY = 3;
    
int main(){
    foo();
    printf("Delay is %d", DELAY);
    return 0;
}

UPDATED: Added working example

Community
  • 1
  • 1
Fusion
  • 5,046
  • 5
  • 42
  • 51
  • 5
    Why is this a problem? If the linker is clever it might strip all the unused code from the result anyway. – Gerhardh Sep 03 '19 at 09:26
  • 2
    @Gerhardh Could be a problem if you define functions with the same name. – Marco Bonelli Sep 03 '19 at 09:31
  • What do you mean by import exactly? Why can't you just use `extern int foo();` (assuming foo returns an int) in `main.c`? – Roflcopter4 Sep 03 '19 at 09:36
  • @Fusion it's somewhat unclear what you're asking here. You should add a minimal working example of those two files so that we can understand what exactly the problem is. – Marco Bonelli Sep 03 '19 at 09:37
  • You don't include .c files. You include .h files and you compile your .c files separately. Finally you link the together witht the linker which will take care of stripping all unused functions. This will solve your problem. – Jabberwocky Sep 03 '19 at 09:42
  • 3
    @Jabberwocky true, inclding a .c is bad practice, but even doing it the right way with an header file would not solve the problem of having functions with the same name redefined in the `main.c` file. – Marco Bonelli Sep 03 '19 at 09:52
  • 1
    I'm afraid you can't do what you want to do. You can get everything from `my_custom_data_structure.c`, or nothing. There is no way to selectively get just one function. If you want to be able to selectively get just one function, you would have to break `my_custom_data_structure.c` up into several separate `.c` files, one per function. (And in that case you wouldn't be able to make use of file-static variables or functions which are private to just the functions in `my_custom_data_structure.c`, if you're doing that.) Also, as others have said, please don't use `#include` with `.c` files. – Steve Summit Sep 03 '19 at 09:52
  • Anyway, what you should do is (1) create a file `my_custom_data_structure.h` containing external declarations and function prototypes for the public functions and variables in `my_custom_data_structure.c`, (2) add the line `#include "my_custom_data_structure.h" at the top of *both* `my_custom_data_structure.c` and `main.c`, (3) do the equivalent of `cc -c main.c`, (4) do `cc -c my_custom_data_structure.c`, and finally (5) do `cc -o myprogram main.o my_custom_data_structure.o`. (Those are Unix/Linux CLI invocations. If you're using Windows, or an IDE, things'll be a little different.) – Steve Summit Sep 03 '19 at 09:56

2 Answers2

0

The usual way is to compile them separately. So you have your main.c, and your extra.c, and you should create a extra.h (and include it in main.c).

In this extra.h, put in declarations for anything that is to be exported from your extra.c file.

For example any functions that should be available to other files. All other functions should be declared/defined only in your extra.c as static, so that they are not available as symbols to be linked when main.c is compiled.

tavish
  • 61
  • 6
0

Normally, you don't include source files (.c) inside other source files (or inside headers). It's possible (and occasionally necessary), but it isn't usual.

Unless you've designed the my_custom_data_structure.c to allow you to specify which functions are to be compiled, you get everything in the file. For example, you could use:

#ifdef USE_FUNCTION1
void function1(void *, …)
{
    …
}
#endif /* USE_FUNCTION1 */

around each function, and then arrange to

#define USE_FUNCTION1

before including the source, but that's not usually a good way of working. It's fiddly. You have to know which functions you use, and which other functions those need, and so on, and it is vulnerable to changes making the lists of USE_FUNCTIONn defines inaccurate. Of course, the source code might have blocks of code like:

#ifdef USE_FUNCTION1
#define USE_FUNCTION37
#define USE_FUNCTION92
#define USE_FUNCTION102
#endif /* USE_FUNCTION1 */

so that if you say you use function1(), it automatically compiles the other functions that are required, but maintaining those lists of definitions is fiddly too, even when the definitions are USE_MEANINGFUL_NAME instead of a number.

Create a header (my_customer_data_structure.h) declaring the functions and any types needed, and split the implementation into many files (mcds_part1.c, mcds_part2.c, …). Compile the separate implementation files into a library (e.g. libmcds.a), and then link your program with the library. If it's a static library, only those functions that are used, directly or indirectly, will be included in the executable.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278