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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:20:58+00:00 2026-05-26T05:20:58+00:00

I am Compiling this using g++ and a makefile. The g++ version is 4.1.2

  • 0

I am Compiling this using g++ and a makefile. The g++ version is 4.1.2 20080704.

Here is the simplified code which still contains the error:

#ifdef __cplusplus
extern "C" 
{
#endif

 class MyClass
  {
    public:
    MyClass() {};
    MyClass& operator=(MyClass&);
    static MyClass& instance() { return log; };

    private:
    static MyClass log;

  }; 

//MyClass MyClass::log;

int main()
{
  MyClass& myClass = MyClass::instance();
  return 0;
}

#ifdef __cplusplus
} //extern "C" 
#endif

When I compile with ‘g++ MyClass.cpp” I get this error:
‘In Function MyClass::Instance()’ : MyClass.cpp : ‘Undefined reference to MyClass::log

If I compile with ‘g++ MyClass.cpp -c’ It builds the object file, but linking that file causes the same error during linking. I tried defining the static variable with “MyClass MyClass::log;” after the class definition, but since it’s compiling it in C, I don’t think that will work. It gives an error message “C++ linkage conflicts with new definition with C linkage”

I’m not sure how to solve this, I’ve been banging my head against this for a few days now. Any help is greatly appreciated.

Extra background info:

The original cpp file this is derived from contains “Simulink-defined functions which must use the C-function syntax.” I’d like to make as few changes as possible to the existing code. I’m not a C programmer, so I have limited understanding here. I do know that these functions are being used with simulink and everything compiles inside the matlab compiler. We are trying to export what simulink created so that we can integrate it in another program, but this is one of the many problems we have compiling. Hopefully that gives some better background info

  • 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-26T05:20:59+00:00Added an answer on May 26, 2026 at 5:20 am

    The declaration of MyClass::log in the class definition gets C++ linkage despite the extern "C", because static class members simply don’t exist in C. But for some reason, the definition of MyClass::log outside the class definition gets C linkage, which doesn’t make much sense to me.

    But you can get around the problem by removing the definition from the scope of the extern "C". Why are you using extern "C"in a C++ file anyway?

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

Sidebar

Related Questions

Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST
When compiling this code on VS2008: #include <vector> using namespace std; class Vertex {
I have this C++ project which compiles using a Makefile, and sometimes when (
Hi upon compiling this code in Visual studio 2008 i get the following error
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
I get an undefined reference to 'typeof'-error compiling and linking this: #include <stdio.h> #include
I get this message when compiling C++ on gcc 4.3 error: ‘NULL’ was not
I get this error when compiling a C# application. Looks like a trivial error,
Consider this simplest piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text;
When compiling a project via a makefile using the Emacs compile command, when gdb

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.