Thread: compilation problem

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    compilation problem

    i have a class name 1.c in tht i am using function n wich has his body in 2.c and declaration in 2.h
    now how can i compile 1.c.
    ex;
    Code:
    1.c
    int main()
    {
    //some data
    n(10);
    //somedata
    }
    *****
    2.c
    int n(int k)
    {
    //some data
    
    }
    int main()
    {
    some data
    }
    *****
    2.h
    int n(int k);
    i complied the progam using command as : cc 1.c -o 1 -lm
    i got the error as;
    1.c.text+0xd5): undefined reference to `n'.
    thank u,
    sree

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to compile in 2.c too. Or compile 2.c with "cc -c 2.c" and link in 2.o with "cc -o 1 1.c 2.o -lm"

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    did you remember to have the #include <2.h> or #include <1.h> header files?

    undefined reference because they are in another file and you need to include them.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    did you remember to have the #include <2.h> or #include <1.h> header files?

    undefined reference because they are in another file and you need to include them.

    No, the error indicated is from the linker, which means that it's compiled correctly, but not completed the linking step.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    thank u for ur response
    i tried to complie 2.c
    i got tht following error.
    (.text+0x18): undefined reference to `main'
    in 2.c no main() function so how can i compile.
    thank u,
    sree

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cnu_sree View Post
    thank u for ur response
    i tried to complie 2.c
    i got tht following error.
    (.text+0x18): undefined reference to `main'
    in 2.c no main() function so how can i compile.
    thank u,
    sree
    Yes, you need "-c" to make it compile on it's own into a .o file - without -c, it tries to link it into an executable file, which assumes your code contains a "main".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or you can do it all on one line (not recommended for larger programs, look into using make)

    cc 1.c 2.c -o 1 -lm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Compilation problem
    By OSDever in forum C++ Programming
    Replies: 10
    Last Post: 09-08-2005, 06:42 AM
  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