# Xcode Test Target - Linker errors, missing headers

**URL:** <https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793>\
**Category:** MacOSX and iOS\
**Created:** [May 21, 2016, 4:28pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793 "2016-05-21T16:28:36Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 21, 2016, 4:28pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/1 "2016-05-21T16:28:36Z")

</div>

Hi folks,

I’m trying to write unit tests in Xcode (7.0.1). I’m aware that Juce has its own unit testing capabilities, but I want to leverage the built-in tooling capabilities of Xcode. This worked prior to my upgrade to Juce 4.2.

As a test, I created an empty Audio Application from Projucer, opened the Xcode project, and added my test target. This creates a file called TestProjectTests.m, which is an Objective-C file. In order to use some Juce classes in my unit tests, I `#include "JuceHeader.h"`, and so I also have to change the file extension to [TestProjectTests.mm](http://TestProjectTests.mm), which permits mixed Objective-C and C++ content in the same file.

Now I run the test, and I get a build error: `/Users/Tom/Documents/Programming/TestProject/JuceLibraryCode/JuceHeader.h:18:10: 'juce_audio_basics/juce_audio_basics.h' file not found`.

The next thing I tried is to add the Juce modules to the header search paths for my test target, exactly like they are in the App target: `../../JuceLibraryCode ../../../JUCE/modules`. Now, when I run the tests I get a linker failure:  
`ld: file not found: /Users/Tom/Library/Developer/Xcode/DerivedData/TestProject-aqkriarcaprkccbjfvtdkacnwhif/Build/Products/Debug/TestProject.app/Contents/MacOS/TestProject clang: error: linker command failed with exit code 1 (use -v to see invocation)`

I have a feeling I’m missing something simple, but I’m clueless as to what to try next. Thoughts? Thanks!!

Edit: I also tried specifying the “User Search Paths” for the test target. This got me a raft of errors in JuceHeader.h, of the flavor:  
`/Users/Tom/Documents/Programming/TestProject/JuceLibraryCode/JuceHeader.h:18:10: 'juce_audio_basics/juce_audio_basics.h' file not found with <angled> include; use "quotes" instead`

Edit #2: I’ve isolated this down further, reproducible with the following steps:  
1: Create an empty Audio Application from Producer  
2: Open the Xcode project  
3: Add a test target  
4: Try to run the tests, get the linker error.

---

<div class="post-metadata">

**Author:** ![cocell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cocell/32/643_2.png) [@cocell](https://forum.juce.com/u/cocell)\
**Post date:** [May 22, 2016, 3:12am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/2 "2016-05-22T03:12:30Z")

</div>

Remove the Debug and Release contents in Test Host.  
Project Settings -\> Test Target -\> Build Settings -\> Test Host -\> Remove Debug and Release Contents

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 22, 2016, 3:59am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/3 "2016-05-22T03:59:25Z")

</div>

Wow - right on the money. Thank you! That got me past the linker issue, and on to another one:

`"juce::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode()", referenced from: ___cxx_global_var_init in TestProjectTests.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)`

Any thoughts on that one?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 22, 2016, 8:21am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/4 "2016-05-22T08:21:35Z")

</div>

```
/*
    As the very long class names here try to explain, the purpose of this code is to cause
    a linker error if not all of your compile units are consistent in the options that they
    enable before including JUCE headers. The reason this is important is that if you have
    two cpp files, and one includes the juce headers with debug enabled, and the other doesn't,
    then each will be generating code with different memory layouts for the classes, and
    you'll get subtle and hard-to-track-down memory corruption bugs!
*/
#if JUCE_DEBUG
 this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode
    ::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode() noexcept {}
#else
 this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_release_mode
    ::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_release_mode() noexcept {}
#endif

}

```

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 22, 2016, 1:32pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/5 "2016-05-22T13:32:10Z")

</div>

Thanks Jules. Digging through the source code for it was the first thing I tried when I saw that, was please to see how well-commented it was, followed by searching the forum, followed by:

1. changing the scheme so both app and test target would build in release. No result.
2. define JUCE\_DEBUG different ways.
3. define FORCE\_NO\_DEBUG different ways.
4. set NDEBUG=1 in Release for my test target

None of these steps had an effect. Clearly I don’t know enough about C++ after having worked with Juce for 2 years. Reading up on what a “compile unit” is didn’t help either. 🙂

---

<div class="post-metadata">

**Author:** ![cocell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cocell/32/643_2.png) [@cocell](https://forum.juce.com/u/cocell)\
**Post date:** [May 22, 2016, 5:25pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/6 "2016-05-22T17:25:04Z")

</div>

Try wrapping [TestProjectTests.mm](http://TestProjectTests.mm) code with this

#ifdef DEBUG  
//headers  
//Methods  
#endif

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 22, 2016, 7:00pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/7 "2016-05-22T19:00:20Z")

</div>

Thanks, will give that a shot in a few days.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 23, 2016, 9:57am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/8 "2016-05-23T09:57:56Z")

</div>

Trying to fix this by defining macros in your headers will probably just make things worse.

The problem only happens if you have different macros defined in different compile units, e.g. by having your own header file somewhere which sets something like \_DEBUG, so that this is picked up by the juce headers when included after this, but not when they’re included by the juce cpp files.

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 23, 2016, 11:37am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/9 "2016-05-23T11:37:02Z")

</div>

Hey Jules, I get what you’re saying, but I’m not sure what you suggest. The fact that this happens in a basically untouched project leads me to believe that there’s something special about the way that the Projucer project is configured which is leading to the issue. Would it help if I attach a reproducer when I get back in a few days?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 23, 2016, 11:51am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/10 "2016-05-23T11:51:47Z")

</div>

Sure, if you can show what it is that you did differently then that’ll probably explain it. But I don’t think this is a problem in the library, it’ll be something like you including a mismatched version of a header file or defining a debug macro somewhere.

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 27, 2016, 1:36am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/11 "2016-05-27T01:36:32Z")

</div>

1. Create a new Projucer AudioApplication, save and open in Xcode.
2. Add a Test Target (OS X Unit Testing Bundle)
3. Run the tests. Fail with linker error.
4. Change Host Application to “None” (removes Test Host info as described above)
5. Run the tests. They succeed.
6. Change file extension on unit test file to “.mm” so C++ can be included with Objective-C
7. #include “JuceHeader.h” in .mm file.
8. Run the tests. Fail with ‘juce\_audio\_basics/juce\_audio\_basics.h’ file not found
9. In Build Settings, copy Header Search Paths from Application Target into Test Target. (…/…/JuceLibraryCode …/…/…/JUCE/modules $(inherited))
10. Run the tests. Fail with juce::this\_will\_fail\_to\_link\_if\_some\_of\_your\_compile\_units\_are\_built\_in\_debug\_mode::this\_will\_fail\_to\_link\_if\_some\_of\_your\_compile\_units\_are\_built\_in\_debug\_mode()

Will attach reproducer.

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 27, 2016, 1:41am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/12 "2016-05-27T01:41:32Z")

</div>

Reproducer here: [https://www.dropbox.com/s/9wt32dng7m25z30/UnitTestIssueReproducer.zip?dl=0](https://www.dropbox.com/s/9wt32dng7m25z30/UnitTestIssueReproducer.zip?dl=0)

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [May 27, 2016, 10:07am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/13 "2016-05-27T10:07:22Z")

</div>

JUCE, by default, puts it’s build products in the build folder next to the xcode project. Xcode by default puts them … well somewhere.

To fix this just change the “Per-configuration Build Products Path” of the unit test target to `$(PROJECT_DIR)/build/$(CONFIGURATION)`.

See screenshots below:

![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/1X/2d45e8dc8f7ef025f627c547edd4fb8224c57662.png)  
 ![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/1X/931a436dd58620b6d0afd097547bd8bc009cf624.png)

 ![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/1X/5c07d4c8956c97e6587ed7a55291686f36ce45fd.png)  
 ![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/1X/368c067ab554d76e96bb488e79393b90cc4fdc85.png)

---

<div class="post-metadata">

**Author:** ![nth](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@nth](https://forum.juce.com/u/nth)\
**Post date:** [May 27, 2016, 11:36pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/14 "2016-05-27T23:36:50Z")

</div>

Awesome - thank you so much, that worked! However, I had to undo Step 4 from my list of steps (Host Application cannot be set to “None”).

It seems reasonable that Juce should put its build products in the same place as Xcode, if things like this don’t work out of the box. But I’m sure you guys have a good reason for doing things the way you do - for my own knowledge, what would that reason be? Also, how did you know how to fix this? Did you just know it from experience, or do you have some troubleshooting technique I can learn from?

---

<div class="post-metadata">

**Author:** ![wminor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/wminor/32/7691_2.png) [@wminor](https://forum.juce.com/u/wminor)\
**Post date:** [April 24, 2017, 10:25pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/15 "2017-04-24T22:25:15Z")

</div>

Apologies for resurrecting a rather old thread, but I’m hitting a similar issue to this, and I’d like to understand _why_ the fix @fabian suggested works. What does the directory that the product gets built in have to do with whether or not `DEBUG` or `NBUGUG` was defined for the juce compliation units vs the test code.

(If my question doesn’t make sense, please enlighten me - I’m pretty new to C++ still and am still finding my way around preprocessing, compilation, and linking, etc.)

Thanks!

---

<div class="post-metadata">

**Author:** ![jacosol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jacosol/32/5080_2.png) [@jacosol](https://forum.juce.com/u/jacosol)\
**Post date:** [December 9, 2018, 4:41pm UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/16 "2018-12-09T16:41:47Z")

</div>

I am facing the same issue when trying to set up google test on a JUCE plugin project (working on Xcode 9.2, OSX 10.12 Sierra, JUCE 5.4.1 ).  
this is my main() inside the “gTest” group I created.

```
#include "gtest.h"
#include "JuceHeader.h"
#include "PluginEditor.h"
#include "PluginProcessor.h"

int main(){
//want to do some testing here on my processor class
}

```

I tried both the solutions suggested in this post but none of them worked. When I try to compile my test target I still get

```
Undefined symbols for architecture x86_64:
  "juce::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode()", referenced from:
      ___cxx_global_var_init in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

```

This happens both if I include the JuceHeader alone or if I only include my project files .

Any ideas/suggestion?

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [December 10, 2018, 9:57am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/17 "2018-12-10T09:57:19Z")

</div>

Are you using Projucer to create the Xcode project? That linker error usually means that either `_DEBUG` is not defined when building a Debug configuration, or that `NDEBUG` is not defined when building a Release configuration. Projucer does that for you, but if you are not using it, you need to do a bit of manual work.

---

<div class="post-metadata">

**Author:** ![jacosol](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jacosol/32/5080_2.png) [@jacosol](https://forum.juce.com/u/jacosol)\
**Post date:** [December 10, 2018, 10:37am UTC](https://forum.juce.com/t/xcode-test-target-linker-errors-missing-headers/17793/18 "2018-12-10T10:37:52Z")

</div>

Thanks for your answering to this old thread @McMartin.

I indeed generated this project with the Projucer. Reading around I indeed learned about those preprocessor definitions you mentioned. I already tried to insert them manually in the build settings of my “Test” target (see below) but did not work.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/c/c0537d4228ff3bf46a6ced5fc68757e8d22becc5.png)  
There were also other entries in the other targets automatically generated by the Projucer and I am not sure if copying those here will also help.  
Maybe I am missing something?  
Also, you think that if I don’t use the projucer and set DEBUG and NDEBUG manually I will not get that error?  
I am happy to provide more screenshots/information if they can help you in understanding the issue here.
