Link to home
Start Free TrialLog in
Avatar of oslicko
oslicko

asked on

Compile C function with no Main Block

I am still learning C, and I wanted to know how exactly do I compile a C program containing a function, but that program doesn't have a Main block? I guess what Im trying to say is, make a function in a separate C file that another C program will somehow call? Kind of like a Java class with its main function calling another java class which only contains functions, no main block...

Here is the one C file that I basically am trying to split up into 2 c files, one with the main block and one containing that void hash_pw function...

/*-------------------------------------------------*/

void   hash_pw(char * key, int size, char * pw);

main() {

char   keyval[] = "POID388631";                      /*  Input buffer. */
char   pwval[11];                       /*  Query String env variable   */

printf("\nit works\n");

strcpy(pwval, keyval);

hash_pw(keyval, strlen(keyval), pwval);

printf("String = %s, Hashed = %s\n", keyval, pwval);
}

void hash_pw( char * key, int size, char * pw ) {

      unsigned long n, q;

      n=0;

      for ( q=0; q<strlen(key); q++ ) {
              n = n*1103515245+ (long)key[q];
      }

      for ( q=0; q<size; q++ ) {
              if (n%36 > 9 ) {
                      *(pw+q) = (char) (n%36+87);
              }
              else {
                      *(pw+q) = (char) (n%36+48);
              }

              n = n*1103515245+12345;
              if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;

      }

      *(pw+size) = (char)0;
      return;
}

Avatar of gj62
gj62

Just put it in a different file, create a .h file (say hash.h) that  includes the function header, e.g.

void   hash_pw(char * key, int size, char * pw);

and include the .h reference in file that has your main(), e.g.

#include <hash.h>

You can then compile it, and include that file in your project (or reference the obj file in your link instructions if not using a development environment like Visual Studio)
Depending on your compiler, compile with the "compile only" flag.  For gcc you'd do:

gcc -c foo.c

and you'd get foo.o containing that code.
You can then link objects together to get an executable like:

gcc foo1.o foo2.o -o foo

to get executable 'foo' (one of the objects need a main function)

ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
whoops - you need a semicolon in HASH.H after the function declaration...

void hash_pw( char * key, int size, char * pw );
Avatar of oslicko

ASKER

I seem to be getting this error:

> gcc main.c
Undefined                       first referenced
 symbol                             in file
hash                                /var/tmp/cc7UrXv5.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status


Here are my files:
-------------HASH.H--------------

#if !defined( HASH_H )
#define HASH_H

void hash( char * key, int size, char * pw );

#endif

-----------END-HASH.H-------------

--------------HASH.C--------------

void hash( char * key, int size, char * pw ) {
 
      unsigned long n, q;
 
      n=0;
 
      for ( q=0; q<strlen(key); q++ ) {
              n = n*1103515245+ (long)key[q];
      }
 
      for ( q=0; q<size; q++ ) {
              if (n%36 > 9 ) {
                      *(pw+q) = (char) (n%36+87);
              }
              else {
                      *(pw+q) = (char) (n%36+48);
              }
 
              n = n*1103515245+12345;
              if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;
 
      }
 
      *(pw+size) = (char)0;
      return;
}

-----------END-HASH.C-------------

--------------MAIN.C--------------

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "hash.h"

main() {

char   keyval[] = "POID388631";                    
char   pwval[11];                      
printf("\nit works\n");

strcpy(pwval, keyval);

hash(keyval, strlen(keyval), pwval);

printf("String = %s, Hashed = %s\n", keyval, pwval);

}

-----------END-MAIN.C-------------

Any Ideas?
Did you compile hash.c and include it in your link?
Avatar of oslicko

ASKER

I actually forgot to compile hash.c . But now Ive tried again and got the following results:

> gcc -c main.c
> gcc -c hash.c
> gcc hash.o -o main
Undefined                       first referenced
 symbol                             in file
main                                /usr/local/gnu/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to main
collect2: ld returned 1 exit status

I don't think its missing anything , here are the directory contents now:

remus> ls *.c *.o
hash.c      hash.o     main.c      main.o


Try

gcc hash.o main.o -o main

Avatar of oslicko

ASKER

Thanks a lot, you've been really helpful!