C: An alternative(better) way to compile files? [duplicate] - c

This question already has answers here:
C++ project organisation (with gtest, cmake and doxygen)
(4 answers)
Closed 6 years ago.
I'm new to C.
I've split my sorting program into three .c files compare_functions.c, insertion_sort.c and main.c, which includes the two.
In order to get the executable, I try the followings: (only related files in the current folder)
gcc -c insertion_sort.c compare_functions.c main.c
gcc *.o -o main.out
In addition to two headers compare_functions.h, insertion_sort.h, finally there are nine files in the folder, after ls them:
compare_functions.c insertion_sort.c main.c
compare_functions.h insertion_sort.h main.o
compare_functions.o insertion_sort.o main.out
Finally the ./main.out just work fine but, say, if I have a program much larger(ie. a hundred of .c file), finally there will be a mess with 100*3 files, is the way described above still suitable?

If you have a lot of source files to compile you might want to check tools like Automake or CMake. They will take care of compiling the right files in the right objects, AND they will only compile the files you edited.

Related

Is there a way to automatically find which files are compiled into a library using cmake/make?

I have a C library which I am using in a project. It consists of .c and .h files in src and include directories. I wrote a CMakeLists.txt file that generates a Makefile which compiles library.so.
The thing is, the library also includes .c files for tests, compatibility headers for other operating systems, and other files which I don't actually use. I would like to determine which src/header files are actually compiled into the .so library. Is there a way to do so automatically, based on CMakeLists.txt or Makefile, without going through and examining each file?
If you are using gccthen you can trick the compiler into telling you which source files it used by generating the dependency information for the next make run. If I am right, the necessary flags are:
-MT $# -MMD -MP -MF $(AUTODEP_DIR)$(notdir $#).d
This should produce for foo.cpp a file foo.o.d which contains target-prerequisites lines like:
foo.o : foo.cpp bar.hpp baz.hpp
foo.cpp :
bar.hpp :
baz.hpp :
where the line with the object file as target (the file before the :) displays what got used as C/C++ source in the compile run (all files after the :). Starting with the list of .o files which are in the libary, this should give you the exact list of files that you are looking for. It requires a bit of scripting but doesn't look too complicated.
clang and other compilers of course have their own, maybe differing set of flags but it shouldn't be too hard to pick them.

how to connect source files and header files in vscode in C

I received 3 files from my teacher, main.c , something.c , something.h .
the header file contains the declarations of the functions.
something.c contains the functions.
main.c contains several calls to the functions.
my question is, how can i run main.c and have everything connected? in python i just import the module and im done (as long the file im importing is saved at the system variables directory or same directory).
thanks
You need to compile both something.c and main.c and link them together in one binary executable file. I don't know which OS are you supposed to run this on, if using Ubuntu, or any linux for that matter you could install gcc or clang to compile your code.
For example:
clang -c something.c main.c
clang something.o main.o
./a.out
First line compiles your files individually (creating main.o and something.o files) Second line links them together creating one executable file (a.out) Third line runs the executable file.

Add a .a library to cmake project [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 4 years ago.
I have a libname.a static library that works fine when I use gcc:
gcc -c main.c -o main.o ;
gcc main.o libname.a main
But now I would like to use CMake as the project is getting big, but I got this message and and I don't know how to include it in an appropriate way. (I tried link_target_library and/or link_directories and/or set(CMAKE_CC_FLAGS "absolute_path/libname.a").
Note that I don't have any source code for libname.a.
/usr/bin/ld: cannot find -llibname
As said in my comment target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/libname.a) works. I can't really tell what the reason for your problem is (maybe you used target_link_library instead of target_link_libraries ?).

When to compile files together and when to use a library [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Search didn't turn anything up.
If I have a file main.c and a pair aux.h and aux.c for useful functions when should I just compile main.c and aux.c together and when should I make a library out of aux (like aux.a).
when should I just compile main.c and aux.c together and when should I make a library out of aux (like aux.a).
When you have a single utility file, putting it into a library buys you nothing. It's only when you have a few separate files, which provide some common, but related functionality that it starts to make sense to put them into an archive (or shared) library.
You actually have 3 choices in building your program:
Compile and link everything together in a single step:
gcc -o main main.c aux.c
Write a proper Makefile. That will result in separate compilation:
gcc -c main.c
gcc -c aux.c
gcc -o main main.o aux.o
Write a Makefile that uses the library. This will also result in separate compilation, something like:
gcc -c main.c
gcc -c aux.c
ar cru libaux.a aux.o
gcc -o main main.o -laux
For anything other than the smallest toy program, you should at least do #2, because doing so eliminates the time wasted recompiling aux.c or main.c when either didn't change, while still ensuring that your program is built correctly.
As I said, doing #3 for a single file buys nothing at all (you just perform a few unnecessary steps).
Using a library is advised when:
You have several different programs that you build, and
You have several related utility files, some (but not all) of which are used in these programs.
Consider the case where you have prog1, prog2 and prog3, and aux1.c, aux2.c and aux3.c utility files. Suppose that prog1 uses code from aux1.c and aux2.c, prog2 uses code from aux1.c and aux3.c, and prog3 uses code from all aux*.c files.
In that situation, using case #2, you will have to write Makefile that looks something like this:
prog1: main1.o aux1.o aux2.o
prog2: main2.o aux1.o aux3.o
prog3: main3.o aux1.o aux2.o aux3.o
That's (potentially) a lot of management.
Compare to the Makefile that uses case #3 (i.e. a library):
prog1: main1.o libaux.a
prog2: main2.o libaux.a
prog3: main3.o libaux.a
Simpler, isn't it?
Also consider what happens if main1.c changes, and starts to use aux3.o as well. In case #2, you would have to adjust your Makefile, but in case #3 your Makefile is already correct!
if the aux.c is used frequently in many other places,you could consider to make it a static or dynamic library.
if it's just a one time run toy program,nothing matters

Compile multiple C files with make

(I am running Linux Ubuntu 9.10, so the extension for an executable is executablefile.out) I am just getting into modular programming (programming with multiple files) in C and I want to know how to compile multiple files in a single makefile. For example, what would be the makefile to compile these files: main.c, dbAdapter.c, dbAdapter.h? (By the way, If you haven't figured it out yet, the main function is in main.c) Also could someone post a link to the documentation of a makefile?
The links posted are all good. For you particular case you can try this. Essentially all Makefiles follow this pattern. Everything else is shortcuts and macros.
program: main.o dbAdapter.o
gcc -o program main.o dbAdapter.o
main.o: main.c dbAdapter.h
gcc -c main.c
dbAdapter.o dbAdapter.c dbAdapter.h
gcc -c dbAdapter.c
The key thing here is that the Makefile looks at rules sequentially and builds as certain items are needed.
It will first look at program and see that to build program, it needs something called main.o and dbAdapter.o.
It will then find main.o. However, to build main.o, it will need main.c and dbAdapter.h (I assume dbAdapter.h is included in main.c).
It will use those sources to build main.o by compiling it using gcc. The -c indicates the we only want to compile.
It does the same thing with dbAdapter.o. When it has those two object files, it is ready to link them. It uses the gcc compiler for this step as well. The -o indicates that we are creating a file called program.
GNU make should be what you're looking for.

Resources