Results 1 to 6 of 6

Thread: Problem in file including.

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem in file including.

    Hi
    I have problem in including file in a class. I have a fuction declared in .h file and define in .c file when i am trying to call it in my .cpp file then it gives error

    error: undefined reference to `packet_formation(unsigned char, unsigned char)'
    while i am including its header file.
    But when by mistake i include its .c file then it is running accurately.
    What is the problem i am not understanding, can we include .c(source file) , is it not unlogical including a source file while the header file has the declaration of function.

    Please help me to understand this.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in file including.

    Is this function declared as extern in header ?
    Is the .c file with function definition compiled and .o linked ?

  3. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in file including.

    no it is not declared with "extern".

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in file including.

    Ok, if I remember correctly "extern" is not required for functions (only for variables existing in another file), so forget about it.
    What about my second question ?

  5. #5
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in file including.

    ya .c file compiled succesfully.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in file including.

    Can you show some relevant code ?
    Simple code using function defined in .c file in .cpp file:
    Qt Code:
    1. // cfunc.h
    2. #ifndef _FUNC_H_
    3. #define _FUNC_H_
    4.  
    5. void func();
    6.  
    7. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // cfunc.c
    2. #include <stdio.h>
    3. #include "cfunc.h"
    4.  
    5. void func(){
    6. printf("works?\n");
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // main.cpp
    2. extern "C"{
    3. #include "cfunc.h"
    4. }
    5.  
    6. int main(){
    7. func();
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Compile & run:
    gcc -c cfunc.c
    g++ main.cpp cfunc.o -o main.exe
    main.exe // prints works ?

Similar Threads

  1. Including and Windows problem
    By isamert in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2011, 01:50
  2. Problem with QGLWidcet including
    By sebap123 in forum Newbie
    Replies: 2
    Last Post: 27th January 2011, 14:21
  3. Qt Creator PROBLEM INCLUDING 'QSystemInfo' HEADER FILE
    By Rakula in forum Qt Tools
    Replies: 1
    Last Post: 24th September 2010, 10:28
  4. Problem with including lib files
    By metow in forum Qt Tools
    Replies: 2
    Last Post: 12th December 2009, 22:57
  5. How to read text file including all empty spaces..
    By umulingu in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2009, 08:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.