views:

52

answers:

3

I have a small project that I need to compile. I have one header and one source that I have created and a nearly empty driver.c that includes my header.
Observe:

// iol.h
#ifndef __IOL_HEADER
#define __IOL_HEADER
/*  program: iol.h
    date:    5 October 2010
*/

#define UNIX 1
#define WINDOWS 2
#define OS UNIX  
#if OS == UNIX
    #include <ncurses.h>
#elif OS == WINDOWS
    #include <conio.h> 
    #include <windows.h>
 // Function declarations!
#endif
void iol_init(void);
#endif

Now my implementation file:

// iol.c
#include <string.h>  
#include <stdlib.h>
#include "iol.h"
void iol_init(void) {
    #if OS == WINDOWS
        /* no startup required for windows */
    #elif OS == UNIX  
        initscr();  
        noecho();  
        cbreak();
        keypad(stdscr, 1);
 // Implmntn continues....  

Now the driver that includes my header and provides the main ():

//main.c
#include "iol.h"

My bash command:

gcc iol.c driver.c -l"ncurses"

I get back:

/tmp/ccmmW6hQ.o:iol.c:(.text+0x83f): first defined here
/tmp/ccwIKUaT.o: In function 'isEscaping':
driver.c:(.text+0xbab): multiple definition of 'isEscaping'
/tmp/ccmmW6hQ.o:iol.c:(.text+0xbab): first defined here
/tmp/ccwIKUaT.o: In function 'initSeq':
..
driver.c:(.text+0x149): undefined reference to 'iol_prnstr'
driver.c:(.text+0x178): undefined reference to 'iol_putch'
..
driver.c:(.text+0x726): undefined reference to 'iol_display'
collect2: ld returned 1 exit status

I just want to get to point where I can compile this, and start ripping my hair out 'cuz of all my seg-faults. What's the problem in my setup? I RTFM on the Gnu C Compiler apparently I'm doing what I'm supposed to, which is declare stuff in iol.h, define in iol.c, and use it in driver.c this is pretty trivial stuff maybe I just need a second set of eyes :S
I'm actually getting a long list of errors, if anyone thinks that's relevant, I'm happy to post the whole source.

A: 

Try compiling them separately:

$ gcc -Wall -c ioi.c
$ gcc -Wall -c driver.c
$ gcc ioi.o driver.o -o program -lncurses

To isolate and fix compilation errors...

Pablo Santa Cruz
I did try that... let me give you the results... Nice thinking but to no avail! Each individual compile-only command executes without an error. Adding the wall switch shows me I'm a bit sloppy: its suggesting I parenthesize my assignments as I decide on their condition, shows me some functions that possibly exit without returns, and show me some unused variables... none of these are going to cause a link error though could they? such that they cause a break in the referencing of the functions? or connecting the declarations with the definitions?
Gus Crawford
A: 

You didn't mention if you are compiling on Windows or Unix. If on Windows I suspect that there are order dependencies in the .h files. Usually you want windows.h first so that it defines constants that the other .h files will use.

verisimilidude
I'm compiling for GCC with a linux distro-
Gus Crawford
+1  A: 

this is the linker complaining. This is what you would get if you had a function defined in the header file that was not declared 'inline'

the missing ones are because you have not added the correct libraries

pm100
hmmm it verbose lets me know if it can't find a library. If I compile gcc ... -lncurses without (in my case on ubuntu anyway) libncurses5 the gcc returns a specific prompt telling me so. I tried gcc ... -lncurses_w, again to no avail- I'll double check but I don't beleive I'm having an issue linking in ncurses but... you never know...
Gus Crawford
@Gus: Why don't you temporarily get rid of all the ncurses stuff, and reduce your problem to the simplest possible case? All this extra stuff is just distracting...
Oli Charlesworth
Okay, well I wrote a simple program with one function and one main() just the same model as above. No link probelms. I go into my header implemenation file and even add some ncurses calls and include the header... compile again with no -lncurses, unsurprisingly, a link error with undefined refs to the ncurses functions.... recompiled with -lncurses... no problems. So my source is germane to the solution indeed... thanks guys.
Gus Crawford
@Gus: Right, so all you need to do is add your original code back in one function at a time, until it fails to link. Then you will have located the problem.
Oli Charlesworth