arrow_back_ios Back to List

Function call within Offload block fails to link!

Offload KB - faq

Old Content Alert

Please note that this is a old document archive and the will most likely be out-dated or superseded by various other products and is purely here for historical purposes.

"Help! A function call from within an Offload™ block is failing to link!"Offload™ blocks require all the function definitions to be present in one C++ file at compile time, as Offload™ requires the function definitions to duplicate the code for SPU.The most common cause of this problem is when a function definition is in another C++ file, that the Offload™ block is not compiling, and thus cannot see the method.

//  Foo.h
struct Foo
{
	int bar();
};

// Foo.cpp
int Foo::bar()
{
	return 42;
}

// Offload.cpp
#include "Foo.h"

static void func()
{
	Foo foo; // fine because we know about struct Foo
	foo.bar(); // fine because we can link against bar in the linker
	__blockingoffload()
	{
		foo.bar(); // fails to link because the Offload compiler could not see the definition
	};
}

The above code fails to SPU-link as the Offload™ compiler cannot see the definition for Foo::bar to compile this function for SPU! There is one of two options available to fix this problem.The first (and most simple) is to move the function definition into the header file. Moving functions into header files will not always work for every case though (and may want to be avoided altogether).The second option is to use an Offload™ unity compile, including Foo.cpp in Offload.cpp. The issue though is that Offload™ will output duplicate function definitions for each method! To overcome this, the Offload compiler provides the -outputoffloadcode command line option (see /kb/13.html for more details) which will output the minimal amount of PPU code required (the code that contains Offload™ blocks), and the duplicated SPU code!The -outputoffloadcode command line option is a very useful and powerful feature, allowing tens or hundreds of C++ files to be included within the one Offload™ file, without causing duplicate functions!