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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:41:21+00:00 2026-06-07T06:41:21+00:00

I have a difference when compiling objective-c source and objective-c++ source. Here a declaration

  • 0

I have a difference when compiling objective-c source and objective-c++ source.

Here a declaration of Class1 and Class2 in test.h :

#import <Foundation/Foundation.h>

@interface Class1 {
}
@end

@interface Class2 {
}
@end

Now, this is Objective-C implementtion in test.m :

#import "test.h"

@implementation Class1
/* static member */
static int mystatic;
@end


@implementation Class2
/* static member */
static int mystatic;
@end

I compile successfully with this command :

gcc -arch armv6 -isysroot /Developer/.../iPhoneOS5.0.sdk -x objective-c -c test.m

Now I use exactly the this Objective-C++ implementation test.mm (exactly same source) :

#import "test.h"

@implementation Class1
/* static member */
static int mystatic;
@end


@implementation Class2
/* static member */
static int mystatic;
@end

And compile with this command line (difference in -x option) :

gcc -arch armv6 -isysroot /Developer/.../iPhoneOS5.0.sdk -x objective-c++ -c test.mm

But I get an error :

test.mm:11 error: redefinition if 'int mystatic'

Why I get this error in ObjC++ and not in ObjC ?

  • 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-07T06:41:25+00:00Added an answer on June 7, 2026 at 6:41 am

    This boils down to the difference between C and C++. In C it is OK to redefine a static variable with the same name and the same type; in C++, doing the same is an error.

    From the C standard:

    A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with a storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definitions for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

    From C++ standard:

    C.1.2, 3.1 Change: C++ does not have “tentative definitions” as in C. E.g., at file scope,

    int i ;
    int i ;
    

    is valid in C, [but it is] invalid in C++.

    As far as Objective C is concerned, the language does not support variables scoped at the class level; declaring static int mystatic; inside an @implementation block has exactly the same effect as declaring it outside the @implementation block. To emulate class-scoped variables, use function-scoped static variables inside class methods.

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

Sidebar

Related Questions

I have the following class hierachy: @interface Message : NSObject {} @end @implementation Message
I thought the difference is that declaration doesn't have parameter types... Why does this
I have been compiling diagrams (pun intended) in hope of understanding the different implementations
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Have seen some similar questions: What is the difference between a JavaBean and a
I have a spot a difference game that every time I solve an image,
I have a question on Date difference in (My)Sql with c#. In my database,
I have algorithm of calculation of the difference between neighboring elements in pure python:
I have to admin there is slight difference between the columns from both tables.
I have two tables and their only difference is one column is missing from

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.