Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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;
}

0
oslicko
Asked:
oslicko
  • 5
  • 3
1 Solution
 
gj62Commented:
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)
0
 
posternbCommented:
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)

0
 
gj62Commented:
So here's what your files would look like:

-----------------------MAIN.C-------------------------
#include "hash.h"

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);
}
----------------------END MAIN.C--------------------------

----------------------HASH.C----------------------------
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;
}
----------------------END HASH.C-------------------------

-----------------------HASH.H----------------------------
#if !defined( HASH_H )
#define HASH_H

void hash_pw( char * key, int size, char * pw )
 
#endif
----------------------END HASH.H-------------------------

The purpose of the define in HASH.H is to allow you to include hash.h in as many source files as needed, and not get errors due to multiple includes.  

You can then use posternb's command-line link options, or simply include the two source (.c) files in your project if using visual studio, etc...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gj62Commented:
whoops - you need a semicolon in HASH.H after the function declaration...

void hash_pw( char * key, int size, char * pw );
0
 
oslickoAuthor Commented:
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?
0
 
gj62Commented:
Did you compile hash.c and include it in your link?
0
 
oslickoAuthor Commented:
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


0
 
gj62Commented:
Try

gcc hash.o main.o -o main

0
 
oslickoAuthor Commented:
Thanks a lot, you've been really helpful!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now