samedi 5 septembre 2015

including header files in the test target environment with Xcode 6.4

I am using Xcode 6.4 in Mac OS X Yosemite. I am trying to test a module (say function.cpp) by including its header file (function.h) in the test target file and testing the functions defined in the module.

This works fine in the 'main.cpp' file. I can include the header file function.h in the 'main.cpp' file and work with the functions defined in function.cpp. But including function.h in the test target file and trying to test the functions in function.cpp by using them in the 'testexample' methods gives an error message when running the test target. More specifically as follows:

function.cpp:

#include "function.h"

int fun(){

int x=2;

return x;

}

function.h:

#ifndef main__function

#define main__function

#include < stdio.h >

int fun();

#endif


main.cpp file:

#include < iostream >

#include "function.h"

int main(int argc, const char * argv[]) { // insert code here...

std::cout << "Hello, World!\n";

std::cout << "value of fun is:" << fun() << "\n";

return 0;

}


test target file named testfunction.m:

#import < Cocoa/Cocoa.h >

#import < XCTest/XCTest.h >

#include "function.h"

@interface testfunction : XCTestCase

@end

...

  • (void)testExample {

    printf("value of fun is:%i\n",fun());

    // This is an example of a functional test case.

    XCTAssert(YES, @"Pass"); }

...

@end


Running the main.cpp code produces:

Hello, World!

value of fun is:2

But running the 'testexample' method above gives the error:

...

"_fun", referenced from:

-[testfunction testExample] in testfunction.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)


The test target environment is a very handy and useful tool for quickly checking and testing the modules of your code however the test target seems to have some linking problems here. Any ideas on how to fix this? Thanks!

Aucun commentaire:

Enregistrer un commentaire