Thread: Confusing problem using Eclipse

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    21

    Unhappy Confusing problem using Eclipse

    Hi,

    I'm a university student taking a module in C programming. While I am enjoying the module, we have recently been set an assignment that has pushed me. I have to use .h files within my code to tie together 4 different questions.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "element_sort.h"
    #include "wavelength.h"
    #include "quadratic.h"
    #include "element_info.h"
    
    
    
    int decision;
    
    int main()
    
    {
    	printf("This program has the ability to do four tasks. "
    			"\n\nPlease pick the corresponding number related"
    			" to the task you wish to be done.\n\n"
    			"1. Sort elements stored on file by either name or abundance.\n"
    			"2. Find maxima from data on wavelengths and absorbances.\n"
    			"3. Root Finding\n"
    			"4. Element Database");
    	while(1)
    
    	{
    			scanf("%i", &decision);
    
    		if (decision == 1)
    		{
    			printf("You have selected option 1");
    			element_sort();
    		}
    		if (decision == 2)
    		{
    			printf("You have selected option 2");
    			wavelength();
    		}
    		if (decision == 3)
    		{
    			printf("You have selected option 3");
    			quadratic();
    		}
    		if (decision == 4)
    		{
    			printf("You have selected option 4");
    			element_info();
    		}
    		else
    		{
    			printf("Invalid Number");
    			continue;
    		}
    		return 0;
    	}
    }
    This is what I have so far. Not much I know, but I've only just started. Each of the .h files that this code 'links' to has something akin to
    Code:
    int wavelength();
    and that alone.

    My problem arises when I try to build I get two errors, with Assessment 2 being the title of the piece.

    make: *** [Assessment2] Error 1 Assessment 2
    symbol(s) not found Assessment 2

    unfortunately these both do not reference anywhere in particular that the error is to be found.
    Please note I have used Eclipse before and it has worked. I also get a similar problem when I attempt to use xcode.
    Any help would be very much appreciated.

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The reason it doesn't reference anywhere in particular is that the problem is you haven't written any of the functions yet. If you have a wavelength.h file with a function prototype in it, then that means you need to have a wavelength.c file with the actual function itself in it. (Of course you can call it what you want, but using the same name for the .h and the .c is customary.)

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Yes I've just created a .c file for each.
    In each .c file it says
    Code:
    #include "wavelength.h"
    and in each .h file it still says as it did before.
    I just need something to placate my compiler so I can run it. Rather than wait til all the code is in. Is there a simple way to do this. Maybe just putting in a return 0; ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every function that is called must be defined.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    I'm sorry, I'm very new to C and I thought I had defined all my functions.

    The error has changed now to this.

    duplicate symbol _element_sort in ./src/element_sort.o and ./src/Assessment 2.o Assessment
    and
    make: *** [Assessment2] Error 1 Assessment 2 0 C/C++ Problem

    I'm not looking for someone to do my work for me, I just need a little help getting out of this rut.
    Thanks in advance

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by typhonius View Post
    Yes I've just created a .c file for each.
    In each .c file it says
    Code:
    #include "wavelength.h"
    and in each .h file it still says as it did before.
    I just need something to placate my compiler so I can run it. Rather than wait til all the code is in. Is there a simple way to do this. Maybe just putting in a return 0; ?
    It is the linker that needs placating, not the compiler. The linker is the program that ensures every attempt to call a function resolves to an actual function.

    You need to provide implementations of the functions you are calling. Or, to use the more formal langage, a definition.

    The declaration in the header tells the compiler the functions are defined (implemented) somewhere. You need to provide the definitions, otherwise you are deceiving the compiler. The linker picks up the deception.

    For example, your build process needs to include a source file (wavelength.c) with the code;
    Code:
    #include <stdio.h>
    #include "wavelength.h"   /*  This contains a DECLARATION of wavelength() */
    
    int wavelength()   /*  this is the DEFINITION of wavelength() */
    {
        printf("Typhonius needs to implement wavelength()\n");
        return -100;
    }
    The requirement then is that, during building, this source file is compiled (to an object file) and later that the linker combines all of the object files to create an executable.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by typhonius View Post
    I'm sorry, I'm very new to C and I thought I had defined all my functions.

    The error has changed now to this.

    duplicate symbol _element_sort in ./src/element_sort.o and ./src/Assessment 2.o Assessment
    and
    make: *** [Assessment2] Error 1 Assessment 2 0 C/C++ Problem

    I'm not looking for someone to do my work for me, I just need a little help getting out of this rut.
    Thanks in advance
    Usually that means that you put your function definition in a header file, but I suppose you could have done something else. In any event, both element_sort.c and Assessment 2.c both claim to have a function called "element_sort" now, which is one too many.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Thanks for the help in amending my code. That has removed some of the minor problems, however Eclipse keeps telling me that "symbols aren't found" and "make: *** [file name] Error 1
    Can anyone shed any light on this?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by typhonius View Post
    Thanks for the help in amending my code. That has removed some of the minor problems, however Eclipse keeps telling me that "symbols aren't found" and "make: *** [file name] Error 1
    Can anyone shed any light on this?
    Read my previous post above.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Yeah I understand now. I was trying to do the main and referencing nothing. I've referenced part of my first equation and it now works.
    Thanks for your help guys. I understand if it is infuriating talking to a dimwit :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM

Tags for this Thread