3

I'm trying to pass a function pointer, but I keep getting a compiler error. I don't normally pass function pointers, but this case demands it. I think that you need only see the declarations to see what I'm doing wrong.

in the header file I have:

pthread_t * createThread(void *(*func)(void *), string arg)

and in the implementation it is the same:

pthread_t * createThread(void *(*func)(void *), string arg)

In the call to this function I'm doing: createThread(&afunction, "ran again")

And the declaration of afunction is:

void *afunction(void *ptr) //(no header, same for both declaration and implementation).

but the compiler spits this out:

    Undefined symbols for architecture x86_64:
  "createThread(void* (*)(void*), std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
      spawnSingleThreadTest()     in threadTests.o
5
  • Where is the definition of createThread and what does it look like? Commented Apr 18, 2012 at 8:12
  • is all of this located in the same file (and how do you build it) ? Commented Apr 18, 2012 at 8:12
  • 3
    It is a linker error, which means, the linker is unable to find the definition of the function. Have you used namespace in your project? Check it out if there is any problem with it. Spelling mistake, or maybe you've used different namespace for declaration and definition? Commented Apr 18, 2012 at 8:12
  • Yah, this is linker problem, not compiler problem Commented Apr 18, 2012 at 8:14
  • This is a linkage problem, surely? Which 'createThead' is it attempting to fixup? 'CreateThread' ? Commented Apr 18, 2012 at 8:15

1 Answer 1

1

You didn't link the implementation to your executable. It's not about function pointer. It's how you compiled your source.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah I see. I have a weird configuration in my eclipse, where I have two build configurations inside a common folder, where I have scriptSRC/scriptMain.cpp -> contains the main function that actually does real work
and scriptSRC/tst/scriptTest.cpp -> a set of unit tests to make sure that things work. The threadCreator.cpp is part of the real functionality, but I want to test it, so I was trying to include "../path to real src" ... For those of you who know what I'm talking about, my build configurations were: script: compile what is in scriptSRC and not in scriptSRC/tst scriptTest: compile what is in scriptSRC/tst. had not configured it for just scriptSRC. Thanks you guys. Sorry I missed that, it was more of a brain drain, I've been working for hours....
I fixed it by editing the configurations in project -> properties -> C/C++ General -> Paths and Symbols -> Source Location I moved the actual build src that does work in scriptSRC/src and the tests in scriptSRC/tst and made sure that the test configuration would not find the main of the src and yet would find the rest of the .cpp files that it needed in order to test, and I made sure of the same for the src main.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.