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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:09:19+00:00 2026-06-08T10:09:19+00:00

I have a simple project broken up into the following source files: my.h: #ifndef

  • 0

I have a simple project broken up into the following source files:

my.h:

#ifndef MY_H
#define MY_H

extern int val;     
void print_val();   

#endif

my.cpp:

#include "my.h"
#include <iostream>

void print_val()
{
    std::cout << val;
}

main.cpp:

#include "my.h"

int main()
{
    val = 4;      
    print_val();  

    return 0;
}

When I compile I receive the following errors:

1>main.obj : error LNK2001: unresolved external symbol "int val" (?val@@3HA)             
1>my.obj : error LNK2001: unresolved external symbol "int val" (?val@@3HA)     
1>c:\...\test.exe : fatal error LNK1120: 1 unresolved externals

Why is this so? I simply want to declare a variable and function in a header and define the function in a separate source file. I use Visual C++ 2010 Express, and the project is a Win32 Console app.

  • 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-08T10:09:21+00:00Added an answer on June 8, 2026 at 10:09 am

    In your my.cpp you probably wanted to define that extern variable of yours, like this:

    int val; // optionally you can also initialize it here, like this: int val=0;
    

    This will make sure the compiler actually allocates an object for this variable that can be later linked into your executable.

    On a related note – make sure there’s only one definition of such extern variable in all of your source files otherwise you’ll have duplicate symbols and linker won’t be happy with that

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

Sidebar

Related Questions

I have a very simple Setup project that copies three dlls into the GAC.
I have a project that is using Spring and is broken down into a
I'd like to try out the D programming language. I have simple pet project
I have a simple Scala project that runs without any problems inside Eclipse, however,
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I have a simple java project (adapted from the example here ), which is
Currently I have a simple maven project that is building a jar file and
Let's say I have two simple models project t.string :title vote t.references :project t.integer
We have created a simple wix project for a basic windows application. Everything builds
In my simple project I have 2 views - a line item view (Brand)

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.