Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:01:31+00:00 2026-05-28T16:01:31+00:00

I have the following constants header: /* constants.h */ #ifdef __cplusplus extern C {

  • 0

I have the following “constants” header:

/* constants.h */

#ifdef __cplusplus 
extern "C" {
#endif

#pragma once

#ifndef CONSTANTS_H
#define CONSTANTS_H

const char * kFoo = "foo";
const char * kBar = "bar";

#endif

#ifdef __cplusplus
}
#endif

I am #include-ing this header in files X.c and Y.c.

Note that I am not including this in X.h or Y.h.

The files X.c and Y.c get compiled into object files which are archived into a static library called libXY.a.

When I include X.h and Y.h in Z.h, and when I link to libXY.a, I cannot compile Z.c without errors:

/* Z.h */

#include "X.h"
#include "Y.h"

I get the following compilation errors when trying to compile Z.c:

/path/to/libXY.a(X.o):(.data+0x0): multiple definition of `kFoo`
/path/to/libXY.a(Y.o):(.data+0x0): first defined here
/path/to/libXY.a(X.o):(.data+0x8): multiple definition of `kBar`
/path/to/libXY.a(Y.o):(.data+0x8): first defined here

I have tried setting kFoo and kBar to extern, but that does not help.

How would I resolve multiple definitions, when I am only including the constants once (via the header guard #ifndef CONSTANTS_H)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:01:33+00:00Added an answer on May 28, 2026 at 4:01 pm

    How would I resolve multiple definitions, when I am only including the constants once (via the header guard #ifndef CONSTANTS_H)?

    With this in constants.h:

    const char * kFoo = "foo";
    

    a definition for kFoo will be emitted in every translation that #includes constants.h. Thus, multiple definitions, which then result in link errors.

    As asaelr noted (+1), you would solve it like this:

    constants.h

    extern const char* const kFoo;
    

    constants.c

    const char* const kFoo = "foo";
    

    (note that i also made the pointer const, which is usually what you want to do in this case)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following header file example: shared_example.h #ifndef SHARED_EX #define SHARED_EX const int Shared_Int
Suppose I have a X.DLL with the following in a header file: extern __declspec(dllexport)
I have a C++ program with the following in the header file: #define VARIABLE_X
if I define my constant varibles in my header like this... extern const double
I have the following class: #pragma once #include <string> #include <iostream> class testclass {
I have the following structure in html5: html - body - header - #wrapper
I have the following in header file. namespace silc{ class pattern_token_map { /* Contents
I have the following html <div class=section> <div>header</div> <div> contents <div>sub contents 1</div> <div>sub
I have a very small header that contains the following code. The code compiles
Let's say I have a hashref constant like the following: use constant DOGS =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.