no error for missing c++ function declaration - g++

With g++, compiling the following code will return error: ‘test’ was not declared in this scope. But Arduino C++ compiler will NOT fail as such. Why? And how can I get gcc/g++ to hoist functions like the Arduino compiler apparently does?
void setup() {
test();
}
void loop() {}
int test() { return 1; }
I've felt for a long time that if Javascript, Perl, etc. can hoist functions, it seems beyond reasonable for the C/C++ pre-processor to allow the same.

I guess you probably need an additional preprocessor for that. I have personally never tried it, but maybe protoize would be interesting for you!

Related

How do clients and DLLs are compatible with respect to calling convnetion

Let's consider the following simple scenario:
// Library.dll
void Foo()
{
// some code
}
// main.c/cpp
int main()
{
// ...
Foo();
// ...
}
First:
From what i know, the caller and the callee must work with the same calling convention. And
if there is a full hand of calling conventions, how is it that we can just link
main.cpp with the Library.dll
and call Foo?
What guarantees that the caller and callee in this case uses the same calling convention?
Is this part of the problems that the COM architecture is trying to solve?
Second: I know there's a way to specify the type of calling convention
when declaring/defining a function. Is there a way to specify
a calling convention to the caller as well?
Clarifiaction
Maybe the question wasn't clear enough.
The question refers to a scenario in which the library was
compiled by some other developer, by an arbitrary compiler.
I compile my own main.cpp with my arbitrary compiler.
How can i be sure that both our compilers "Speak the same language"
with respect to calling conventions?

Variable is not a static member of class

I'm working on a GameKitHelper class, and it's mostly written in C++, but with Objective-C in some places as well, inside an .mm file.
I removed a bit of functionality to isolate the error:
void GameKitHelper::PopulateFriendScores(DynArray<GameCenterScore> *FriendScores)
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardRequest.range = NSMakeRange(1,25);
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error)
{
int i = 0;
printf("%d", i);
}];
}
}
The error I get here is:
'int GameKitHelper::i' is not a static member of 'class GameKitHelper'
This is a gcc bug. See Objective-C++ block vs Objective-C block for one of many reports of it.
<soapbox>I recommend avoiding Objective-C++ as much as possible. It's slow to compile, bloated to run (particularly with ARC since it turns on -fobjc-arc-exceptions), buggy in the compiler and the debugger, and mostly a mess in my experience giving the worst of both worlds. C++ is fine. Objective-C is fine. Just keep the interface between them as small as possible. </soapbox>
But switching to clang 2.0 might fix this specific problem.
In case it might help someone else...
I am required by the needs of my project to use the 10.6 SDK and LLVM-gcc 4.2. I cannot require clang for the code to compile.
I worked around this problem by declaring my variable as a shared_ptr in the parent function... putting the actual object I need on the heap. In the block, I access the object through the shared_ptr variable. This arrangement causes the shared_ptr to be implicitly copied into the block while the copy in the parent function is free to be released. Since I am not declaring a variable in the block, I bypass the bug.
I used a preprocessor check to use a normal stack variable if the code is building on clang or some other compiler.

What is this objective-c function construct called?

I was looking through one of Apple's XCode tutorials and came across something that looked like this inside the implementation of a class method:
void (^foo)(void);
foo = ^(void) {
NSLog(#"Hello, World!");
};
foo();
Now obviously this is some kind of function declaration, implementation, and usage. However, I'd like to know more about it, what it is called, and what its limitations and advantages are. My searches online are turning up nothing relevant. Can anyone point me in the proper direction?
They're called blocks. You can think of a block as a chunk of code that you can pass around to other parts of your program. They were added by Apple to its C and Objective-C compilers relatively recently, but some newer APIs take blocks instead or in addition to selectors or function pointers.
Blocks and block variables.
Here's some reading:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html
http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1
http://thirdcog.eu/pwcblocks/

Objective-C reference to a pointer declaration getting error

I had this declaration.
- (BOOL)getNHPData:(REMOTE_MESS_ID)msgId withEvent:(RSEvent*&)pEvent;
I tried with RSEvent** also but i'm getting this error for 2 times
Expected ')' before RSEvent
Why is it so.
Objective-C is a superset of the C language and does not have references. If you want to use C++-style references in Objective-C, you must compile as Objective-C++ (as you might expect, Objective-C++ is a superset of C++). Use the .mm extension to automatically use the Objective-C++ in Xcode.
If the method in question is a public API that will be consumed from Objective-C, I would highly recommend using a pointer-to-pointer (RSEvent**) instead of a pointer reference. Using Objective-C++ in a header "infects" clients with Objective-C++ (unless you're very careful). Objective-C++ takes much longer to compile that Objective-C and you will eventually run into the inevitable C vs. C++ incompatibilities. Standard practice is to hide Objective-C++ from public APIs whenever possible.
I personally have never had much success in c++ or obj-c with pointer references, if I need that kind of functionality, I usually just use a pointer pointer like this:
some function()
{
RSEvent *pEvent = new RSEvent();
[self getNHPData:DEFAULT_MSG_ID withEbvent:&pEvent];
}
- (BOOL)getNHPData:(REMOTE_MESS_ID)msgId withEvent:(RSEvent**)pEvent
{
//Do some stuff
}

pthread_cancel behaves differently on arm and ppc?

I'm currently working on a multi-threaded application that would be deployed on arm and ppc architecture. I'm having some problem with pthread_cancel on arm.
pthread_cancel on arm doesn't behave the same with ppc. The thread gets cancelled but the destructor for the thread's local variable isn't being called on arm. I also tried explicitly defining a cancellation cleanup handler routine installed via pthread_cleanup_push. But it isn't being called when the thread is cancelled.
The code works fine with ppc. When a thread is cancelled, local variable's destructor is being called. And when I explicitly defined a cleanup handler, it was called and executed when pthread_cancel was called.
Am I missing something? Some compiler options perhaps?
Programming Language: C++
Compilers: arm-linux-g++/powerpc-linux-g++
OS: Linux
EDIT:
I have found a sort of similar problem logged on this libc bug.
Using gcc instead of g++ and adding -fno-exception compiler option did the trick. But I really want to understand stuff behind this issue. Moreover, the -fno-exception means I won't be able to perform exception handling in my application, not that I'm using it now but I might be in the future.
Thanks.
Thread cancellation without the help from the application is a bad idea. Just google. It is much better to tell the thread to end itself by setting a flag variable that is periodically checked by the thread.
Actually cancellation is so hard that it has been omitted from the latest C++0x draft. You can search http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html and won't find any mention of cancellation at all. Here's the definition of the proposed thread class (you won't find cancel there):
class thread
{
public:
// types:
class id;
typedef implementation-defined native_handle_type; // See [thread.native]
// construct/copy/destroy:
thread();
template <class F> explicit thread(F f);
template <class F, class ...Args> thread(F&& f, Args&&... args);
~thread();
thread(const thread&) = delete;
thread(thread&&);
thread& operator=(const thread&) = delete;
thread& operator=(thread&&);
// members:
void swap(thread&&);
bool joinable() const;
void join();
void detach();
id get_id() const;
native_handle_type native_handle(); // See [thread.native]
// static members:
static unsigned hardware_concurrency();
};