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 7928565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:46:08+00:00 2026-06-03T19:46:08+00:00

I am getting an undefined reference when trying to use variables from my namespace.

  • 0

I am getting an undefined reference when trying to use variables from my namespace. I created a header file and an implementation file with the namespace in it and am trying to use the namespace in another file…

EDITED:

//first.h
namespace first
{
  extern int var;
  extern int multiplyVar(int);
}

//first.cpp
namespace first
{
  int var = 5;
  int multiplyVar(int mult)
  {
    return mult * var;
  }
}

//someOtherFile.h
#include "first.h"

//someOtherFile.cpp
first::var = 3;
int newVar = first::multiplyVar(3);

//error
undefined reference to  'first::multiplyVar(...)'
undefined reference to 'first::var'

EDIT Actual Code

//jFork.h
#ifndef JFORK_H
#define JFORK_H

#include <iostream>
#include <string>

using namespace std;

namespace jFork
{
  extern int sockfd, newsockfd;
  int j_fork(string);
}

#endif //JWDSFORK_H

//jFork.cpp
namespace jFork
{
  int sockfd = 0, newsockfd = 0;

  int j_fork(string name)
  {
    cout<<"Preparing to fork: "<<name<<endl;

    int rv = fork();

    cout<<"Called fork(): "<<name<<endl;

    switch(rv)
    {
    case -1:
        cout<<"Exiting..."<<endl;
        exit(EXIT_FAILURE);
        break;
    case 0:
        if(sockfd)
        {
            cout<<"Closing sockfd: "<<name<<endl;
            close(sockfd);
            sockfd = 0;
        }

        if(newsockfd)
        {
            cout<<"Closing newsockfd: "<<name<<endl;
            close(newsockfd);
            newsockfd = 0;
        }

        break;
    default:
        cout<<"Preparing to sleep: "<<name<<endl;
        sleep(1);
        cout<<"Woke up from sleep"<<name<<endl;
        break;
    }

    return rv;
  }
}

//server.cpp
int pid = jFork::j_fork(name);
  • 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-06-03T19:46:10+00:00Added an answer on June 3, 2026 at 7:46 pm

    Note no extern for function declrations and defining the symbols in the namespace in the implementation file.

    //first.h
    namespace first
    {
      extern int var;
      extern int multiplyVar(int);
    }
    
    //first.cpp
    var = 5;
    extern int multiplyVar(int mult)
    {
      return mult * var;
    }
    

    Should be

    //first.h
    namespace first
    {
      extern int var;
      int multiplyVar(int);
    }
    
    //first.cpp
    namespace first
    {
       int var = 5;
       int multiplyVar(int mult)
       {
         return mult * var;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting a Can't use an undefined value as a HASH reference error trying
Why am I getting an undefined reference error using gcc? I am trying to
I am getting undefined reference to 'mysql_suchandsuch@#' messages when trying to link my program
I am trying to call cplus_demangle() function from libiberty.a, but I am getting undefined
I'm getting undefined reference when I build an application in Windows witn mingw32 (it
I'm getting the following errors trying to compile a project: (fortran, using gfortran) undefined
I am getting an error message that says undefined reference to encrypt(int, std::list<int, std::allocator<int>
When trying to compile with Xcode, I am getting the following error: Undefined symbols
I am trying to link several object files and I am getting 3 undefined
Am getting this error for my code: Undefined symbols for architecture x86_64: _spendDollars, referenced

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.