Thread: How to define, declare and initialize variables in another library?

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    29

    How to define, declare and initialize variables in another library?

    Hello,


    I've been mostly doing my libraries with embedded projects which is quite easy. But last night I wanted to test an accessor function which can initialize the functions in the other libraries.

    So I decided to test that in CODE::BLOCKS, but I had some issues preparing and linking a library, which worked after couple attempts.

    Then I wanted to declare a variable in that library but I got an error! Why? and what to do now?

    My setup is as follows:

    1. I have a folder for the application program which contains the main function.
    2. There's another folder beside the application program folder and contains the test library. And I defined and also declared a variable in this library but I got the error.

    How to define, declare and initialize variables in another library?-codeblock_library_problem-jpg

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    The error message says something about macro expansion. You need to post more code, especially the code that uses the macro named car.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    29
    Quote Originally Posted by laserlight View Post
    The error message says something about macro expansion. You need to post more code, especially the code that uses the macro named car.
    It's actually the one in the picture which is pointed by the red mark, that's where is I'm defining that name.

    And calling it here:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <inttypes.h>
    #include <string.h>
    #include "library_source_example.h"
    
    
    int main()
    {
        printf("%d\n",car);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    29
    I'm sorry wait ... it's my mistake now. I redefined another variable as
    Code:
    int car;

    But keeping this thread now for the near incoming issues I get in developing libraries in code blocks. My goal is to develop a function that sets the timing for other functions in other libraries.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    Quote Originally Posted by wolfrose
    It's actually the one in the picture which is pointed by the red mark, that's where is I'm defining that name.
    That doesn't make sense though. There's absolutely nothing wrong with that macro definition as can be seen from what you have posted.

    Quote Originally Posted by wolfrose
    And calling it here:
    This looks perfectly fine too.

    Could you post the smallest and simplest program that you expect should compile, but which demonstrates this problem? Post in code tags, not an image: actually, your current code might suffice, but post the entire program so that we can copy and paste to try to compile it too. Also, post the full error message in code tags as well.

    EDIT:
    Ah, I see. This is a lesson you should learn: use the convention of fully uppercase macro names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    29
    Quote Originally Posted by laserlight View Post
    EDIT:
    Ah, I see. This is a lesson you should learn: use the convention of fully uppercase macro names.
    Sorry! My mistake.

    I'm looking now for a solution to call other functions from other libraries which is easy, just include the library and call its function.

    But now, I'm looking to develop a function which I can pass to it functions' names and the time required to activate that function. And that's what I don't know, is how to pass a function name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to declare and initialize string variables in C?
    By UncleBazerko in forum C Programming
    Replies: 2
    Last Post: 09-19-2018, 07:54 AM
  2. Declare vs. Define
    By Euclid365BCE in forum C Programming
    Replies: 4
    Last Post: 10-17-2015, 09:17 AM
  3. How to define/declare inline functions (C99 Rules)
    By ardavirus in forum C Programming
    Replies: 5
    Last Post: 11-10-2011, 03:01 AM
  4. declare and initialize struct members
    By Mr.Bit in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 11:57 AM
  5. Why Declare in .H but Define in .CPP ?
    By Krak in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 12:36 AM

Tags for this Thread