4

I've written several c text processing functions that I've placed in a the files: string_functions.c and string_functions.h.

I was using these functions for one project and that worked out well. Now I want to use these same functions in a completely different project at the same time. I'm using gcc in Debian.

Is there a good way to use the same c source code in multiple projects at the same time. The projects are in different sub-directories with the same parent directory.

How do I structure the make files to do this?

Or do I just place a copy of the string_functions.c(h) in both projects. This seems like it would make it harder to maintain the source code.

2
  • If you're not used to writing makefiles, I'd recommend you SCons. It is a much easier to use build system, and you use it to define separate build descriptions for your main projects and your shared code. Commented Aug 25, 2014 at 1:35
  • If the usage and compilation settings are the same, the single source should be compiled to a static or shared library. It is feasible (and in fact I have had happen) that a single source files is compiled in to different projects of entirely different nature (one 32bit DLL, another 64bit unrelated EXE), In such a case, including the same source as source in both is warranted. You simply have to remember the intermediate output (the object code) for that shared source file must be kept separate as multiple outputs (one for each project) will be generated, and they are not compatible. Commented Aug 25, 2014 at 1:38

2 Answers 2

3

Best way to do this is to build your C files (.h and .c) into a shared library.

There are many tutorials available on how to do this with gcc; one is at this link

Once the shared library is built, you can then link it into many other projects.

Briefly, these are the steps.

Ensure your string_functions.c includes string_functions.h and builds, of course.

Then compile position independent (that's what -fPIC is for)

$gcc -Wall -fPIC -c string_functions.c

Finally build your shared library like this

$gcc -shared -o my_stringfunctions.so string_functions.o

To link to your new shared library from some other program, ensure that whatever directory you put it in is in your LD_LIBRARY_PATH.

Then you may link using something like

$gcc  my_otherprogram.c -L/path/to/my/lib -lmy_stringfunctions

As pointed out, one should put include files (.h) used by a shared library in some directory path, and add the location to the include search path using the -I option:

$gcc  my_otherprogram.c -I/path/to/include/files -L/path/to/my/lib -lmy_stringfunctions
Sign up to request clarification or add additional context in comments.

1 Comment

This is fine but it's only half of the answer ... you should also talk about putting the header file in a common location and including it with -I.
0

If this is how your directory looks:

/parent
    /project1
        ...
        string_functions.h
        string_functions.c
    /project2
        ...
        string_functions.h
        string_functions.c

Then all you have to do is store it in a common location, and then point to that location when building your code. This is the standard way of doing it for custom installed libraries in /opt/, for example.

Hence, one suggestion is to do your directory structure like this:

/parent
    /include
        string_functions.h
        string_functions.c
    /project1
        ...
    /project2
        ...

And when building your respective projects, you include that search path when compiling (using the -I flag):

gcc mainfile.c -I/parent/include <other options>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.