c - how to create a config object











up vote
2
down vote

favorite












I want to create a config object that can be used anywhere in my c program.



What would be the best practice to do so?



Currently, I have a config.h that looks like this:



#define OUTPUT 0
#define OUTPUT_DISPLAY 0
#define OUTPUT_WIDTH 1920
#define OUTPUT_HEIGHT 1080

typedef struct {
const char *output
const char *output_display;
int output_width;
int output_height;
} config_t;


Would I create a config_t instance named config or something in the header file?



Thanks










share|improve this question


























    up vote
    2
    down vote

    favorite












    I want to create a config object that can be used anywhere in my c program.



    What would be the best practice to do so?



    Currently, I have a config.h that looks like this:



    #define OUTPUT 0
    #define OUTPUT_DISPLAY 0
    #define OUTPUT_WIDTH 1920
    #define OUTPUT_HEIGHT 1080

    typedef struct {
    const char *output
    const char *output_display;
    int output_width;
    int output_height;
    } config_t;


    Would I create a config_t instance named config or something in the header file?



    Thanks










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to create a config object that can be used anywhere in my c program.



      What would be the best practice to do so?



      Currently, I have a config.h that looks like this:



      #define OUTPUT 0
      #define OUTPUT_DISPLAY 0
      #define OUTPUT_WIDTH 1920
      #define OUTPUT_HEIGHT 1080

      typedef struct {
      const char *output
      const char *output_display;
      int output_width;
      int output_height;
      } config_t;


      Would I create a config_t instance named config or something in the header file?



      Thanks










      share|improve this question













      I want to create a config object that can be used anywhere in my c program.



      What would be the best practice to do so?



      Currently, I have a config.h that looks like this:



      #define OUTPUT 0
      #define OUTPUT_DISPLAY 0
      #define OUTPUT_WIDTH 1920
      #define OUTPUT_HEIGHT 1080

      typedef struct {
      const char *output
      const char *output_display;
      int output_width;
      int output_height;
      } config_t;


      Would I create a config_t instance named config or something in the header file?



      Thanks







      c config






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 23:27









      yasgur99

      131214




      131214
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          You declare a global object in the header file:



          extern config_t global_config;


          and then define it in some suitable .c file:



          config_t global_config;


          If you were to define the config variable in the header file, the linker would complain that multiple instances of global_config exist, as a new instance would get created with each import (assuming your project has multiple .c files).






          share|improve this answer






























            up vote
            3
            down vote













            Create a config.c file that you will add to your compilation with the following:



            #include <config.h> // or "config.h"
            config_t config_instance = {
            .output = "default output",
            .output_display = "default display",
            };


            And extern the varaible in your config.h file :



            extern config_t config_instance;


            Or better. Let's create a function that will allow access to your config, so we can track and validate changes to it. Create config.c like this:



            #include <config.h> // or "config.h"
            static config_t config_instance = {
            .output = "default output",
            .output_display = "default display",
            };

            const config_t *config_get(void) {
            return &config_instance;
            }

            int config_set(const config_t *newconfig) {
            if (/* check if newconfig is valid etc. */) {
            return -1; // return error number
            }
            config_instance = *newconfig;
            return 0; // success in changing config
            }


            and export the functions by adding in your config.h file:



             const config_t *config_get(void);
            int config_set(const config_t *newconfig);





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217684%2fc-how-to-create-a-config-object%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote













              You declare a global object in the header file:



              extern config_t global_config;


              and then define it in some suitable .c file:



              config_t global_config;


              If you were to define the config variable in the header file, the linker would complain that multiple instances of global_config exist, as a new instance would get created with each import (assuming your project has multiple .c files).






              share|improve this answer



























                up vote
                5
                down vote













                You declare a global object in the header file:



                extern config_t global_config;


                and then define it in some suitable .c file:



                config_t global_config;


                If you were to define the config variable in the header file, the linker would complain that multiple instances of global_config exist, as a new instance would get created with each import (assuming your project has multiple .c files).






                share|improve this answer

























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  You declare a global object in the header file:



                  extern config_t global_config;


                  and then define it in some suitable .c file:



                  config_t global_config;


                  If you were to define the config variable in the header file, the linker would complain that multiple instances of global_config exist, as a new instance would get created with each import (assuming your project has multiple .c files).






                  share|improve this answer














                  You declare a global object in the header file:



                  extern config_t global_config;


                  and then define it in some suitable .c file:



                  config_t global_config;


                  If you were to define the config variable in the header file, the linker would complain that multiple instances of global_config exist, as a new instance would get created with each import (assuming your project has multiple .c files).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 at 23:33









                  PSkocik

                  31.4k54569




                  31.4k54569










                  answered Nov 8 at 23:31









                  Henning Koehler

                  1,100610




                  1,100610
























                      up vote
                      3
                      down vote













                      Create a config.c file that you will add to your compilation with the following:



                      #include <config.h> // or "config.h"
                      config_t config_instance = {
                      .output = "default output",
                      .output_display = "default display",
                      };


                      And extern the varaible in your config.h file :



                      extern config_t config_instance;


                      Or better. Let's create a function that will allow access to your config, so we can track and validate changes to it. Create config.c like this:



                      #include <config.h> // or "config.h"
                      static config_t config_instance = {
                      .output = "default output",
                      .output_display = "default display",
                      };

                      const config_t *config_get(void) {
                      return &config_instance;
                      }

                      int config_set(const config_t *newconfig) {
                      if (/* check if newconfig is valid etc. */) {
                      return -1; // return error number
                      }
                      config_instance = *newconfig;
                      return 0; // success in changing config
                      }


                      and export the functions by adding in your config.h file:



                       const config_t *config_get(void);
                      int config_set(const config_t *newconfig);





                      share|improve this answer

























                        up vote
                        3
                        down vote













                        Create a config.c file that you will add to your compilation with the following:



                        #include <config.h> // or "config.h"
                        config_t config_instance = {
                        .output = "default output",
                        .output_display = "default display",
                        };


                        And extern the varaible in your config.h file :



                        extern config_t config_instance;


                        Or better. Let's create a function that will allow access to your config, so we can track and validate changes to it. Create config.c like this:



                        #include <config.h> // or "config.h"
                        static config_t config_instance = {
                        .output = "default output",
                        .output_display = "default display",
                        };

                        const config_t *config_get(void) {
                        return &config_instance;
                        }

                        int config_set(const config_t *newconfig) {
                        if (/* check if newconfig is valid etc. */) {
                        return -1; // return error number
                        }
                        config_instance = *newconfig;
                        return 0; // success in changing config
                        }


                        and export the functions by adding in your config.h file:



                         const config_t *config_get(void);
                        int config_set(const config_t *newconfig);





                        share|improve this answer























                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          Create a config.c file that you will add to your compilation with the following:



                          #include <config.h> // or "config.h"
                          config_t config_instance = {
                          .output = "default output",
                          .output_display = "default display",
                          };


                          And extern the varaible in your config.h file :



                          extern config_t config_instance;


                          Or better. Let's create a function that will allow access to your config, so we can track and validate changes to it. Create config.c like this:



                          #include <config.h> // or "config.h"
                          static config_t config_instance = {
                          .output = "default output",
                          .output_display = "default display",
                          };

                          const config_t *config_get(void) {
                          return &config_instance;
                          }

                          int config_set(const config_t *newconfig) {
                          if (/* check if newconfig is valid etc. */) {
                          return -1; // return error number
                          }
                          config_instance = *newconfig;
                          return 0; // success in changing config
                          }


                          and export the functions by adding in your config.h file:



                           const config_t *config_get(void);
                          int config_set(const config_t *newconfig);





                          share|improve this answer












                          Create a config.c file that you will add to your compilation with the following:



                          #include <config.h> // or "config.h"
                          config_t config_instance = {
                          .output = "default output",
                          .output_display = "default display",
                          };


                          And extern the varaible in your config.h file :



                          extern config_t config_instance;


                          Or better. Let's create a function that will allow access to your config, so we can track and validate changes to it. Create config.c like this:



                          #include <config.h> // or "config.h"
                          static config_t config_instance = {
                          .output = "default output",
                          .output_display = "default display",
                          };

                          const config_t *config_get(void) {
                          return &config_instance;
                          }

                          int config_set(const config_t *newconfig) {
                          if (/* check if newconfig is valid etc. */) {
                          return -1; // return error number
                          }
                          config_instance = *newconfig;
                          return 0; // success in changing config
                          }


                          and export the functions by adding in your config.h file:



                           const config_t *config_get(void);
                          int config_set(const config_t *newconfig);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 23:33









                          Kamil Cuk

                          7,2541222




                          7,2541222






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217684%2fc-how-to-create-a-config-object%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Schultheiß

                              Verwaltungsgliederung Dänemarks

                              Liste der Kulturdenkmale in Wilsdruff