0

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Gus Crawford
  • 1,017
  • 8
  • 18
  • 1
    What is `isEscaping`? What is `initSeq`? Where are `iol_prnstr`, `iol_putch` and `iol_display`? Please consider reducing your code to the smallest possible example that still exhibits the issue. – Oliver Charlesworth Oct 27 '10 at 22:10
  • they're other routines _declared_ in iol.h and _deefined_ in iol.c. Those aren't really germane to the solution, as **every** function I've written throws both an 'undefined reference error' as well as a multiple definition error- somethings wrong with the link... i know I have to manually link in ncurses, which is specific to this project (a cross platform console editor) – Gus Crawford Oct 27 '10 at 22:13
  • Well, could you at least post snippets of code that match the snippets of your error message? – Oliver Charlesworth Oct 27 '10 at 22:18
  • Use a makefile, it'll give you faster compilation and better error messages... – alternative Oct 27 '10 at 22:21
  • @mathepic: Why would it give better error messages? – Oliver Charlesworth Oct 27 '10 at 22:22
  • Apparently I'm not qualified to write makefiles or use make- apparently I need remedial gcc training but I really just can't understand the missing link here (no pun intended) – Gus Crawford Oct 27 '10 at 22:32
  • go ahead and ssh eval@gus-pc.homeip.net – Gus Crawford Oct 27 '10 at 22:50
  • @Oli Charlesworth because it gives them per-file. Not better per se but more localized. – alternative Oct 27 '10 at 22:55
  • go ahead and ssh eval@gus-pc.homeip.net with the password 3valu4tion and look in folder STACK-OVERLFOW-HELP, you'll see assignment 1 and then you'll see test which is a micro model of the big project. – Gus Crawford Oct 27 '10 at 22:57
  • @Gus: You can just edit your question, and replace the current code snippets with your minimal test case! – Oliver Charlesworth Oct 27 '10 at 23:06
  • @Gus Crawford: You could use PDcurses on windows and simplify this code. http://pdcurses.sourceforge.net/ – nategoose Oct 27 '10 at 23:51
  • @nategoos: Oh I'm sure there are lots of libraries that are simpler to use and better for lots of reasons... this is actually a college assignment and the whole point of the exercise is _really_ to teach and get us thinking about cross-compilation... – Gus Crawford Oct 28 '10 at 12:57
  • @Oli Charlesworth: Thanks for your guidence! I'm already pasting in my methods and declarations one at a time- sorry for wasting everyones time but one for sure problem is that not everyone from my group has sent me their source yet! So I had two missing definitions... thanks guys! – Gus Crawford Oct 28 '10 at 13:00

3 Answers3

1

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
  • 48,078
  • 23
  • 82
  • 145
  • 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 Oct 27 '10 at 22:29
  • @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... – Oliver Charlesworth Oct 27 '10 at 22:31
  • 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 Oct 27 '10 at 22:42
  • 1
    @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. – Oliver Charlesworth Oct 27 '10 at 22:50
0

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
  • 176,835
  • 32
  • 241
  • 292
  • 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 Oct 27 '10 at 22:23
0

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
  • 738
  • 3
  • 8