Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Dev-C++ .h files

luhnthluhnth Posts: 78Member
Whenever I try to get my projects organized using .h files, I get a million linker errors. How can I stop this from happening?

Comments

  • MT2002MT2002 Posts: 1,444Member
    : Whenever I try to get my projects organized using .h files, I get a million linker errors. How can I stop this from happening?
    :
    [blue]
    What linker errors are you getting?

    Two common problems is not having include gaurds (You will get
    alot of "multiple definition" errors without them.

    The other one is using globals from other source files without
    giving them external linkage (You will get "unresolved external"
    errors here.[/blue]
  • luhnthluhnth Posts: 78Member
    [b][red]This message was edited by luhnth at 2007-3-19 17:11:59[/red][/b][hr]
    I get an error like this:

    [Linker error] undefined reference to `TTF_Init'

    How can I stop this? It gets really annoying to be forced to keep every single function into main.cpp.


    Best Regards,
    CodeKing



  • MT2002MT2002 Posts: 1,444Member
    : [b][red]This message was edited by luhnth at 2007-3-19 17:11:59[/red][/b][hr]
    : I get an error like this:
    :
    : [Linker error] undefined reference to `TTF_Init'
    :
    : How can I stop this? It gets really annoying to be forced to keep every single function into main.cpp.
    :
    :
    : Best Regards,
    : CodeKing
    :
    :
    :
    :
    [blue]
    What *.h files do you include?

    Is TTF_Init part of a library, or another source file that you wrote?

    Can you please post the include section of your files, and
    let us know what libraries you are linking.
    [/blue]
  • LundinLundin Posts: 3,711Member
    [b]Quick and dirty way:[/b]

    [code]
    /* myfile.h */

    #ifndef _MYFILE_H /* header guard */
    #define _MYFILE_H

    extern int someVariable;

    void someFunction (void);

    #endif[/code]

    [code]
    /* myfile.c */
    #include "myfile.h"

    int someVariable;

    void someFunction (void)
    {
    }[/code]

    [code]
    /* main.c */
    #include "myfile.h"

    someVariable = x; /* access variable */
    someFunction(); /* call function */[/code]


    [b]Good programming practice / encapsulation:[/b]

    [code]
    /* myfile.h */

    #ifndef _MYFILE_H /* header guard */
    #define _MYFILE_H

    int getSomeVariable (void);
    void setSomeVariable (int x);

    #endif[/code]

    [code]
    /* myfile.c */
    #include "myfile.h"

    static int someVariable;

    int getSomeVariable (void)
    {
    return someVariable;
    }

    void setSomeVariable (int x)
    {
    someVariable = x;
    }[/code]

    [code]
    /* main.c */
    #include "myfile.h"

    x = getSomeVariable(); /* read variable */
    setSomeVariable(x); /* write to variable*/
    [/code]
  • luhnthluhnth Posts: 78Member
    Thank you, that will prove handy, but I get the linker errors with my functions, not the variables. I know I have to create a .lib or .a file for myfile.h. How do I do this? Or if there's some other way, how do I do that?

    : [b]Quick and dirty way:[/b]
    :
    : [code]
    : /* myfile.h */
    :
    : #ifndef _MYFILE_H /* header guard */
    : #define _MYFILE_H
    :
    : extern int someVariable;
    :
    : void someFunction (void);
    :
    : #endif[/code]
    :
    : [code]
    : /* myfile.c */
    : #include "myfile.h"
    :
    : int someVariable;
    :
    : void someFunction (void)
    : {
    : }[/code]
    :
    : [code]
    : /* main.c */
    : #include "myfile.h"
    :
    : someVariable = x; /* access variable */
    : someFunction(); /* call function */[/code]
    :
    :
    : [b]Good programming practice / encapsulation:[/b]
    :
    : [code]
    : /* myfile.h */
    :
    : #ifndef _MYFILE_H /* header guard */
    : #define _MYFILE_H
    :
    : int getSomeVariable (void);
    : void setSomeVariable (int x);
    :
    : #endif[/code]
    :
    : [code]
    : /* myfile.c */
    : #include "myfile.h"
    :
    : static int someVariable;
    :
    : int getSomeVariable (void)
    : {
    : return someVariable;
    : }
    :
    : void setSomeVariable (int x)
    : {
    : someVariable = x;
    : }[/code]
    :
    : [code]
    : /* main.c */
    : #include "myfile.h"
    :
    : x = getSomeVariable(); /* read variable */
    : setSomeVariable(x); /* write to variable*/
    : [/code]
    :


    Best Regards,
    CodeKing

  • LundinLundin Posts: 3,711Member
    : Thank you, that will prove handy, but I get the linker errors with my functions, not the variables. I know I have to create a .lib or .a file for myfile.h. How do I do this? Or if there's some other way, how do I do that?

    No you don't need a lib. Just make a project in Dev and add all C files to it.

    Note: Most IDE:s only allow you to add the files to be linked to the project. That is [b]only the C files and not the h-files[/b]. I'm not sure how picky Dev is with this, but better safe than sorry.
  • luhnthluhnth Posts: 78Member
    [b][red]This message was edited by luhnth at 2007-3-21 15:44:49[/red][/b][hr]
    I fixed the linker errors with the "ttf" I just created a new .h file (Been a long time) and I'm getting errors like:

    multiple definition of `InitVideo(unsigned int)'

    Here's my code:

    main.cpp

    [code]#include // For some useful functions such as atexit :)
    #include // main SDL header
    #include //For sound mixing
    #include //For fonts and text
    #include //For network
    #include

    #include "initSDL.h"

    #define SCREEN_WIDTH 640
    #define SCREEN_HEIGHT 480

    SDL_Surface *screen; //This pointer will reference the backbuffer#include

    using namespace std;



    int main(int argc, char *argv[])
    {
    if (!InitVideo(SDL_DOUBLEBUF | SDL_FULLSCREEN)) {
    return EXIT_FAILURE;
    }
    printf("Going good");
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    [/code]

    initSDL.h

    [code]#ifndef _INITSDL_H_
    #define _INITSDL_H_

    #include // main SDL header

    int InitVideo(Uint32 flags); //Init video
    int InitAudio(); //Init audio
    int InitTTF(); //Init fonts
    int InitNet(); //Init networking
    SDL_Surface* getScreen(); //Get screen

    #endif
    [/code]

    initSDL.cpp

    [code]#include // For some useful functions such as atexit :)
    #include // main SDL header
    #include //For sound mixing
    #include //For fonts and text
    #include //For network

    #include "initSDL.h"

    #define SCREEN_WIDTH 640
    #define SCREEN_HEIGHT 480

    using namespace std;

    SDL_Surface *screen; //This pointer will reference the backbuffer#include

    int InitVideo(Uint32 flags) { //Initialize video
    // Load SDL
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "Unable to initialize SDL: %s
    ", SDL_GetError());
    return false;
    }
    atexit(SDL_Quit); // Clean it up nicely :)

    // fullscreen can be toggled at run time :) any you might want to change the flags with params?
    //set the main screen to SCREEN_WIDTHxSCREEN_HEIGHT with a colour depth of 16:
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, flags);
    if (screen == NULL) {
    fprintf(stderr, "Unable to set video mode: %s
    ", SDL_GetError());
    return false;
    }
    return true;
    }

    int InitAudio() { //Initialize audio
    //Load Audio Support
    if(SDL_Init(SDL_INIT_AUDIO) != 0) {
    fprintf(stderr, "Warning: unable to initialize audio: %s
    ", SDL_GetError());
    return false;
    }

    if (Mix_OpenAudio(11025, AUDIO_S16, 2, 512) < 0) {
    fprintf(stderr, "Warning: Audio could not be setup for 11025 Hz 16-bit stereo.
    Reason: %s
    ", SDL_GetError());
    return false;
    }
    return true;
    }

    int InitTTF() { //Initialize Text and Fonts
    if(TTF_Init()==-1) {
    fprintf(stderr, "Error: unable to initialize TTF_SDL, %s
    ", TTF_GetError());
    return false;
    }
    atexit(TTF_Quit);
    return true;
    }

    int InitNet() { //Initialize networking
    if(SDLNet_Init()==-1) {
    fprintf(stderr, "Error: unable to initialize SDL networking, %s
    ", SDLNet_GetError());
    return false;
    }
    atexit(SDLNet_Quit);
    return true;
    }

    SDL_Surface* getScreen()
    {
    return screen;
    }[/code]

    Best Regards,
    CodeKing



  • MT2002MT2002 Posts: 1,444Member
    : [b][red]This message was edited by luhnth at 2007-3-21 15:44:49[/red][/b][hr]
    : I fixed the linker errors with the "ttf" I just created a new .h file (Been a long time) and I'm getting errors like:
    :
    : multiple definition of `InitVideo(unsigned int)'
    :
    : Here's my code:
    :
    : main.cpp
    :
    : [code]#include // For some useful functions such as atexit :)
    : #include // main SDL header
    : #include //For sound mixing
    : #include //For fonts and text
    : #include //For network
    : #include
    :
    : #include "initSDL.h"
    :
    : #define SCREEN_WIDTH 640
    : #define SCREEN_HEIGHT 480
    :
    : SDL_Surface *screen; //This pointer will reference the backbuffer#include
    :
    : using namespace std;
    :
    :
    :
    : int main(int argc, char *argv[])
    : {
    : if (!InitVideo(SDL_DOUBLEBUF | SDL_FULLSCREEN)) {
    : return EXIT_FAILURE;
    : }
    : printf("Going good");
    : system("PAUSE");
    : return EXIT_SUCCESS;
    : }
    : [/code]
    :
    : initSDL.h
    :
    : [code]#ifndef _INITSDL_H_
    : #define _INITSDL_H_
    :
    : #include // main SDL header
    :
    : int InitVideo(Uint32 flags); //Init video
    : int InitAudio(); //Init audio
    : int InitTTF(); //Init fonts
    : int InitNet(); //Init networking
    : SDL_Surface* getScreen(); //Get screen
    :
    : #endif
    : [/code]
    :
    : initSDL.cpp
    :
    : [code]#include // For some useful functions such as atexit :)
    : #include // main SDL header
    : #include //For sound mixing
    : #include //For fonts and text
    : #include //For network
    :
    : #include "initSDL.h"
    :
    : #define SCREEN_WIDTH 640
    : #define SCREEN_HEIGHT 480
    :
    : using namespace std;
    :
    : SDL_Surface *screen; //This pointer will reference the backbuffer#include
    :
    : int InitVideo(Uint32 flags) { //Initialize video
    : // Load SDL
    : if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    : fprintf(stderr, "Unable to initialize SDL: %s
    ", SDL_GetError());
    : return false;
    : }
    : atexit(SDL_Quit); // Clean it up nicely :)
    :
    : // fullscreen can be toggled at run time :) any you might want to change the flags with params?
    : //set the main screen to SCREEN_WIDTHxSCREEN_HEIGHT with a colour depth of 16:
    : screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, flags);
    : if (screen == NULL) {
    : fprintf(stderr, "Unable to set video mode: %s
    ", SDL_GetError());
    : return false;
    : }
    : return true;
    : }
    :
    : int InitAudio() { //Initialize audio
    : //Load Audio Support
    : if(SDL_Init(SDL_INIT_AUDIO) != 0) {
    : fprintf(stderr, "Warning: unable to initialize audio: %s
    ", SDL_GetError());
    : return false;
    : }
    :
    : if (Mix_OpenAudio(11025, AUDIO_S16, 2, 512) < 0) {
    : fprintf(stderr, "Warning: Audio could not be setup for 11025 Hz 16-bit stereo.
    Reason: %s
    ", SDL_GetError());
    : return false;
    : }
    : return true;
    : }
    :
    : int InitTTF() { //Initialize Text and Fonts
    : if(TTF_Init()==-1) {
    : fprintf(stderr, "Error: unable to initialize TTF_SDL, %s
    ", TTF_GetError());
    : return false;
    : }
    : atexit(TTF_Quit);
    : return true;
    : }
    :
    : int InitNet() { //Initialize networking
    : if(SDLNet_Init()==-1) {
    : fprintf(stderr, "Error: unable to initialize SDL networking, %s
    ", SDLNet_GetError());
    : return false;
    : }
    : atexit(SDLNet_Quit);
    : return true;
    : }
    :
    : SDL_Surface* getScreen()
    : {
    : return screen;
    : }[/code]
    :
    : Best Regards,
    : CodeKing
    : [blue]
    Can you please post all of your errors?[/blue]
  • luhnthluhnth Posts: 78Member
    : : [b][red]This message was edited by luhnth at 2007-3-21 15:44:49[/red][/b][hr]
    : : I fixed the linker errors with the "ttf" I just created a new .h file (Been a long time) and I'm getting errors like:
    : :
    : : multiple definition of `InitVideo(unsigned int)'
    : :
    : : Here's my code:
    : :
    : : main.cpp
    : :
    : : [code]#include // For some useful functions such as atexit :)
    : : #include // main SDL header
    : : #include //For sound mixing
    : : #include //For fonts and text
    : : #include //For network
    : : #include
    : :
    : : #include "initSDL.h"
    : :
    : : #define SCREEN_WIDTH 640
    : : #define SCREEN_HEIGHT 480
    : :
    : : SDL_Surface *screen; //This pointer will reference the backbuffer#include
    : :
    : : using namespace std;
    : :
    : :
    : :
    : : int main(int argc, char *argv[])
    : : {
    : : if (!InitVideo(SDL_DOUBLEBUF | SDL_FULLSCREEN)) {
    : : return EXIT_FAILURE;
    : : }
    : : printf("Going good");
    : : system("PAUSE");
    : : return EXIT_SUCCESS;
    : : }
    : : [/code]
    : :
    : : initSDL.h
    : :
    : : [code]#ifndef _INITSDL_H_
    : : #define _INITSDL_H_
    : :
    : : #include // main SDL header
    : :
    : : int InitVideo(Uint32 flags); //Init video
    : : int InitAudio(); //Init audio
    : : int InitTTF(); //Init fonts
    : : int InitNet(); //Init networking
    : : SDL_Surface* getScreen(); //Get screen
    : :
    : : #endif
    : : [/code]
    : :
    : : initSDL.cpp
    : :
    : : [code]#include // For some useful functions such as atexit :)
    : : #include // main SDL header
    : : #include //For sound mixing
    : : #include //For fonts and text
    : : #include //For network
    : :
    : : #include "initSDL.h"
    : :
    : : #define SCREEN_WIDTH 640
    : : #define SCREEN_HEIGHT 480
    : :
    : : using namespace std;
    : :
    : : SDL_Surface *screen; //This pointer will reference the backbuffer#include
    : :
    : : int InitVideo(Uint32 flags) { //Initialize video
    : : // Load SDL
    : : if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    : : fprintf(stderr, "Unable to initialize SDL: %s
    ", SDL_GetError());
    : : return false;
    : : }
    : : atexit(SDL_Quit); // Clean it up nicely :)
    : :
    : : // fullscreen can be toggled at run time :) any you might want to change the flags with params?
    : : //set the main screen to SCREEN_WIDTHxSCREEN_HEIGHT with a colour depth of 16:
    : : screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, flags);
    : : if (screen == NULL) {
    : : fprintf(stderr, "Unable to set video mode: %s
    ", SDL_GetError());
    : : return false;
    : : }
    : : return true;
    : : }
    : :
    : : int InitAudio() { //Initialize audio
    : : //Load Audio Support
    : : if(SDL_Init(SDL_INIT_AUDIO) != 0) {
    : : fprintf(stderr, "Warning: unable to initialize audio: %s
    ", SDL_GetError());
    : : return false;
    : : }
    : :
    : : if (Mix_OpenAudio(11025, AUDIO_S16, 2, 512) < 0) {
    : : fprintf(stderr, "Warning: Audio could not be setup for 11025 Hz 16-bit stereo.
    Reason: %s
    ", SDL_GetError());
    : : return false;
    : : }
    : : return true;
    : : }
    : :
    : : int InitTTF() { //Initialize Text and Fonts
    : : if(TTF_Init()==-1) {
    : : fprintf(stderr, "Error: unable to initialize TTF_SDL, %s
    ", TTF_GetError());
    : : return false;
    : : }
    : : atexit(TTF_Quit);
    : : return true;
    : : }
    : :
    : : int InitNet() { //Initialize networking
    : : if(SDLNet_Init()==-1) {
    : : fprintf(stderr, "Error: unable to initialize SDL networking, %s
    ", SDLNet_GetError());
    : : return false;
    : : }
    : : atexit(SDLNet_Quit);
    : : return true;
    : : }
    : :
    : : SDL_Surface* getScreen()
    : : {
    : : return screen;
    : : }[/code]
    : :
    : : Best Regards,
    : : CodeKing
    : : [blue]
    : Can you please post all of your errors?[/blue]
    :

    multiple definition of `function(vars)'
    first defined here

    for every function in initSDL.h and initSDL.cpp


    Best Regards,
    CodeKing

  • luhnthluhnth Posts: 78Member
    [b][red]This message was edited by luhnth at 2007-3-24 8:29:16[/red][/b][hr]
    : : : [b][red]This message was edited by luhnth at 2007-3-21 15:44:49[/red][/b][hr]
    : : : I fixed the linker errors with the "ttf" I just created a new .h file (Been a long time) and I'm getting errors like:
    : : :
    : : : multiple definition of `InitVideo(unsigned int)'
    : : :
    : : : Here's my code:
    : : :
    : : : main.cpp
    : : :
    : : : [code]#include // For some useful functions such as atexit :)
    : : : #include // main SDL header
    : : : #include //For sound mixing
    : : : #include //For fonts and text
    : : : #include //For network
    : : : #include
    : : :
    : : : #include "initSDL.h"
    : : :
    : : : #define SCREEN_WIDTH 640
    : : : #define SCREEN_HEIGHT 480
    : : :
    : : : SDL_Surface *screen; //This pointer will reference the backbuffer#include
    : : :
    : : : using namespace std;
    : : :
    : : :
    : : :
    : : : int main(int argc, char *argv[])
    : : : {
    : : : if (!InitVideo(SDL_DOUBLEBUF | SDL_FULLSCREEN)) {
    : : : return EXIT_FAILURE;
    : : : }
    : : : printf("Going good");
    : : : system("PAUSE");
    : : : return EXIT_SUCCESS;
    : : : }
    : : : [/code]
    : : :
    : : : initSDL.h
    : : :
    : : : [code]#ifndef _INITSDL_H_
    : : : #define _INITSDL_H_
    : : :
    : : : #include // main SDL header
    : : :
    : : : int InitVideo(Uint32 flags); //Init video
    : : : int InitAudio(); //Init audio
    : : : int InitTTF(); //Init fonts
    : : : int InitNet(); //Init networking
    : : : SDL_Surface* getScreen(); //Get screen
    : : :
    : : : #endif
    : : : [/code]
    : : :
    : : : initSDL.cpp
    : : :
    : : : [code]#include // For some useful functions such as atexit :)
    : : : #include // main SDL header
    : : : #include //For sound mixing
    : : : #include //For fonts and text
    : : : #include //For network
    : : :
    : : : #include "initSDL.h"
    : : :
    : : : #define SCREEN_WIDTH 640
    : : : #define SCREEN_HEIGHT 480
    : : :
    : : : using namespace std;
    : : :
    : : : SDL_Surface *screen; //This pointer will reference the backbuffer#include
    : : :
    : : : int InitVideo(Uint32 flags) { //Initialize video
    : : : // Load SDL
    : : : if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    : : : fprintf(stderr, "Unable to initialize SDL: %s
    ", SDL_GetError());
    : : : return false;
    : : : }
    : : : atexit(SDL_Quit); // Clean it up nicely :)
    : : :
    : : : // fullscreen can be toggled at run time :) any you might want to change the flags with params?
    : : : //set the main screen to SCREEN_WIDTHxSCREEN_HEIGHT with a colour depth of 16:
    : : : screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, flags);
    : : : if (screen == NULL) {
    : : : fprintf(stderr, "Unable to set video mode: %s
    ", SDL_GetError());
    : : : return false;
    : : : }
    : : : return true;
    : : : }
    : : :
    : : : int InitAudio() { //Initialize audio
    : : : //Load Audio Support
    : : : if(SDL_Init(SDL_INIT_AUDIO) != 0) {
    : : : fprintf(stderr, "Warning: unable to initialize audio: %s
    ", SDL_GetError());
    : : : return false;
    : : : }
    : : :
    : : : if (Mix_OpenAudio(11025, AUDIO_S16, 2, 512) < 0) {
    : : : fprintf(stderr, "Warning: Audio could not be setup for 11025 Hz 16-bit stereo.
    Reason: %s
    ", SDL_GetError());
    : : : return false;
    : : : }
    : : : return true;
    : : : }
    : : :
    : : : int InitTTF() { //Initialize Text and Fonts
    : : : if(TTF_Init()==-1) {
    : : : fprintf(stderr, "Error: unable to initialize TTF_SDL, %s
    ", TTF_GetError());
    : : : return false;
    : : : }
    : : : atexit(TTF_Quit);
    : : : return true;
    : : : }
    : : :
    : : : int InitNet() { //Initialize networking
    : : : if(SDLNet_Init()==-1) {
    : : : fprintf(stderr, "Error: unable to initialize SDL networking, %s
    ", SDLNet_GetError());
    : : : return false;
    : : : }
    : : : atexit(SDLNet_Quit);
    : : : return true;
    : : : }
    : : :
    : : : SDL_Surface* getScreen()
    : : : {
    : : : return screen;
    : : : }[/code]
    : : :
    : : : Best Regards,
    : : : CodeKing
    : : : [blue]
    : : Can you please post all of your errors?[/blue]
    : :
    :
    : multiple definition of `function(vars)'
    : first defined here
    :
    : for every function in initSDL.h and initSDL.cpp
    :
    :
    : Best Regards,
    : CodeKing
    :
    :

    I didn't look at my errors close enough. It turns out that the errors are only for the functions with no parameters. I put void inside the (), but that didn't help. I put SDL_Surface *screen into them and it compiled fine. Is there any way to not have to pass a useless parameter.

    Best Regards,
    CodeKing



Sign In or Register to comment.