0

For my program I am linking 3 files in total. A main.c, sortfile.c and my.h(header file). For my sortfile.c I am implementing a OddEven Sort. I am unsure whether my coding algorithm is correct. Also would like to know what information usually goes in a header file. Is it only the other two c files vide #include?

#include <stdio.h>


void swap(int *, int *);
void Odd_Even_Sort(int *);


/* swaps the elements */
void swap(int * x, int * y)
{
    int temp;

    temp = *x;
    *x = *y;
    *y = temp;
}

/* sorts the array using oddeven algorithm */
void Odd_Even_Sort(int * x)
{
    int sort = 0, i;

    while (!sort)
    {
        sort = 1;
        for (i = 1;i < MAX;i += 2)
        {
            if (x[i] > x[i+1])
            {
                swap(&x[i], &x[i+1]);
                sort = 0;
            }
        }
        for (i = 0;i < MAX - 1;i += 2)
        {
            if (x[i] > x[i + 1])
            {
                swap(&x[i], &x[i + 1]);
                sort = 0;
            }
        }
    }

I did not include a main in the sortfile.c because I intended to put main in the main.c file.

josmar
  • 57
  • 5
  • 1
    What do you need help with? Do you get compilation errors? – giusti Jan 28 '17 at 22:43
  • @giusti As to how I would link this to my main.c, haven't compiled this code as of yet – josmar Jan 28 '17 at 22:48
  • Your original question is unclear. This is a Q&A site. Rewrite the question to make it specific and answerable. – DYZ Jan 28 '17 at 22:54
  • 1
    Write a *Makefile* and run `make`. You may, of course, compile each `*.c` module separately, like `gcc -O2 -Wall -o x.o x.c` and then link result `gcc -o prog x.o y.o` – 0andriy Jan 28 '17 at 22:55

1 Answers1

2

You look confused. Read first the wikipage on linkers and on compilers. You don't link source files, but only object files and libraries.

(I am guessing and supposing and hoping for you that you are using Linux)

You also compile translation units into object files.

Header files are for the preprocessor (the first "phase" of the compilation). The preprocessing is a textual operation. See this answer for some hint.

So you probably want to compile your main.c into main.o with

gcc -Wall -Wextra -g -c main.c -o main.o

(the -Wall asks for all warnings, so never forget that; the -Wextra asks for more of them; the -g asks for debugging information; -c asks to compile some source into some object file; order of program arguments to gcc matters a big lot).

Likewise, you want to compile your sortfile.c into sortfile.o. I leave as an exercise to get the right command doing that.

Finally, you want to get an executable program myprogsort, by linking both object files. Do that with

gcc -g main.o sortfile.o -o myprogsort

But you really want to use some build automation tool. Learn about GNU make. Write your Makefile (beware, tabs are important in it). See this example. Don't forget to try make -p to understand (and take advantage of) all the builtin rules make is knowing.

Learn to use some version control software, e.g. git, for your source code and tests.

Consider also using code generators (like GNU bison for parsing tasks) if and when relevant. Repetitive code could be generated by preprocessors (e.g. GPP).

Also would like to know what information usually goes in a header file.

Conventionally you want only declarations in your common header file (which you would #include in every source file composing a translation unit). You can also add definitions of static inline functions. Read more about inline functions (you probably don't need them at first).

Don't forget to learn how to use the gdb debugger. You probably will run

gdb ./myprogsort

more than once. Don't forget to rebuild your thing after changes to source code.

Once you are confident with your code and have debugged simple bugs, you would compile it with optimization flags like -O2 (in addition, or in place of -g). This is needed to benchmark your executable. Learn about profiling.

Some programs are designed to accept plugins (extending their features). On Linux or POSIX systems, this uses dlopen(3) and dlsym(3). Other programs cooperate with other executables and communicate with them (e.g. carefully using popen(3), sockets, RPC/XDR, JSONRPC, etc....)

Look also inside the source code of some medium sized free software project coded in C on github. You'll learn a big lot.

NB. On serious software projects senior developers would decide on some coding guidelines (e.g. GNU standards), rules and conventions, and others would review the code, perhaps even developing some GCC plugin to validate some of these coding rules. Be however aware of Rice's theorem.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you so much for the response, also I was instructed to display the amount of numbers in the file at index [0] for the array(a[ ]) then the preceding numbers. For example, lets say there are 5 numbers in the file. In the array it should be `a[0]5 a[1]1 a[2]2 a[3]3 a[4]4 a[5]5`. Any idea on how to code that? – josmar Jan 29 '17 at 00:04
  • No, I won't do your homework. As I suggested, study the source code of some medium sized free software project. *Read* some *existing* code before writing your own. And please consider that you'll learn a lot more by doing *yourself* a homework, than by asking others to do it. You need to learn (not to have your homework done by others). – Basile Starynkevitch Jan 29 '17 at 07:32