Thread: Can't figure this out

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    28

    Unhappy Can't figure this out

    Hello, I'm working on an exercise in my book. It tells you to write a program that will print out the first n prime numbers, where n is inputed by the user.

    My output is should like this (which it does):

    Primes
    --------
    2
    3
    5
    7
    ...


    The program should have 3 files, a prime.h which has all the preprocessing directives, a is_prime.c file which has the function to determine if a number is prime and prime.c which contains the main function.

    Here's what all three looks like:

    prime.h

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int is_prime( int n );
    is_prime.c

    Code:
    #include "prime.h"
    
    
    int is_prime( int n ){
    	int k, limit;
    	
    	if( n == 2) return 1;
    	if( n % 2 == 0) return 0;
    	limit = n / 2;
    	for( k = 3 ; k <= limit ; k += 2 )
    		if( n % k == 0 ) return 0;
    	return 1;
    }
    and finally prime.c (the main program)
    Code:
    #include "prime.h"
    
    
    int main(void){
    	int i=2, n, count=0;
    	
    	printf("Howw many prime number do you want to see? ");
    	scanf("%d", &n );
    	
    	printf("Primes\n------\n");
    
    	while(1){	
    		if( is_prime( i ) ){
    			count++;
    			printf("%3d: %d \n", count, i);
    		}
    		i++;
    		if( count == n ) break;	
    	}
    	return 0;
    }
    My problem is, when I compile with these 3 files I get an error:

    Unresolved external '_is_prime' referenced from prime.obj

    but if I put all the code in one file it works fine, what am I doing wrong?
    Dat
    http://hoteden.com <-- not an porn site damnit!

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Try using 'extern' to make your function available.
    Code:
    extern int is_prime( int n )
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    include this statement in your main program(prime.c)


    #include "is_prime.h"

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    Originally posted by aqua
    include this statement in your main program(prime.c)


    #include "is_prime.h"
    i put the line:
    Code:
    #include "is_prime.c"
    in my prime.c and it works, but shouldn't it work anyways since I already included the line:

    Code:
    #include "prime.h"
    Right???
    Dat
    http://hoteden.com <-- not an porn site damnit!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 10:43 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 09:42 AM