title: xcode ios project , how to import cpp code files from other directory?
body: <p>I tried to put the CPP codes in a folder for unified management,<br />
  and provide them for Android and iOS platforms to compile and use together.</p>
  <p>I've searched the Internet for several examples ,<br />
  but <strong>none of them provide detailed steps</strong>.</p>
  <hr />
  <p>The directory structure for my experiment is as follows :</p>
  <pre><code>TestCppCrossPlatorm
   |---AndroidDemo     // for AndroidStudio to create a AndroidDemo
   |
   |---CommonCPP       // cpp files 
     |---Core.h
     |---Core.cpp
     |---myMath
       |---NumAdd.h
       |---NumAdd.cpp 
   |
   |---iOS_Demo       // for Xcode to create a iOS project
  </code></pre>
  <hr />
  <pre><code>AndroidDemo
   |---app
      |---src
        |---main
           |---cpp
              |---CMakeLists.txt
              |---native-lib.cpp
           |---java
           |---res
  </code></pre>
  <p>A demo project created by AndroidStudio,
  I configure in <code>CMakeFiles.txt</code> to add <code>CommonCPP</code> code files as follow :</p>
  <pre><code>
  include_directories(../../../../../CommonCPP)
  
  
  add_library( # Sets the name of the library.
               native-lib
  
               # Sets the library as a shared library.
               SHARED
  
               # Provides a relative path to your source file(s).
               native-lib.cpp
              ../../../../../CommonCPP/Core.h
              ../../../../../CommonCPP/Core.cpp
  
              ../../../../../CommonCPP/myMath/NumAdd.h
              ../../../../../CommonCPP/myMath/NumAdd.cpp
          )
  </code></pre>
  <p>Edit <code>native-lib.cpp</code> :</p>
  <pre><code>#include &quot;Core.h&quot;            // test cpp code : Core.h
  #include &quot;myMath/NumAdd.h&quot;   // test cpp code : NumAdd.h
  
  extern &quot;C&quot; JNIEXPORT jstring JNICALL
  Java_sodino_demo_MainActivity_stringFromJNI(
          JNIEnv* env,
          jobject /* this */) {
  
      // code to try :  it's ok!
      std::string addResult = std::to_string(add(2, 3));
      const char* result = concatenateMyStringWithCppString(addResult.c_str());
  //    std::string hello = &quot;Hello from C++&quot;;
      return env-&gt;NewStringUTF(result);
  }
  </code></pre>
  <p>The code is working fine, and the demo is also running OK.</p>
  <hr />
  <p><strong>There are some problems with the configuration of the iOS project</strong></p>
  <p>A iOS demo project is created in the folder iOS_Demo by Xcode,</p>
  <p>First, I add a <code>static library</code> named CommonCPP for iOS_Demo by the path <code>Build Settings -&gt; Targets -&gt; Add</code>,</p>
  <p>Second, click <code>CommonCPP</code> target, select <code>Build Phases -&gt; Compile Sources -&gt; Add</code>, add <code>CommonCPP</code> folder, as follow :</p>
  <p><a href="https://i.stack.imgur.com/7RdNI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7RdNI.png" alt="enter image description here" /></a></p>
  <p>Edit <code>ViewController.mm</code> to add <code>include Core.h and myMath/NumAdd.h</code>,</p>
  <p>It failed ...
  <code>'myMath/NumAdd.h' file not found</code><br />
  <a href="https://i.stack.imgur.com/frl5Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/frl5Z.png" alt="enter image description here" /></a></p>
  <p>Question 1 :<br />
  Why is all cpp files displayed in the Xcode below <code>Demo_iOS</code> instead of the <code>CommonCPP</code> target?</p>
  <p>Question 2 :<br />
  Why <code>Core.h</code> can be include ok, but <code>myMath/NumAdd.h</code> failed? and how to fixed it?</p>
  <hr />
  <p>examples without detailed steps : <a href="https://gusc.lv/2021/03/how-to-use-c-for-cross-platform-development/" rel="nofollow noreferrer">How to use C++ for Cross-Platform Development</a></p>
  <p>CMAKE is a good choice
  But now, I'm more concerned about how to do it <strong>step by step, and right</strong>.</p>
  <hr />
  <p>Core.h</p>
  <pre><code>#ifndef __HelloCpp__Core__
  #define __HelloCpp__Core__
  
  #include &lt;iostream&gt;
  
  const char *concatenateMyStringWithCppString(const char *myString);
  
  #endif /* defined(__HelloCpp__Core__) */
  
  </code></pre>
  <p>Core.cpp</p>
  <pre><code>#include &lt;string.h&gt;
  #include &quot;Core.h&quot;
  #include &quot;myMath/NumAdd.h&quot;
  
  const char *CPP_BASE_STRING = &quot;cpp says hello to %s&quot;;
  
  const char *concatenateMyStringWithCppString(const char *myString) {
      char *concatenatedString = new char[strlen(CPP_BASE_STRING) + strlen(myString)];
      sprintf(concatenatedString, CPP_BASE_STRING, myString);
  
  
      return concatenatedString;
  }
  
  </code></pre>
  <p>myMath/NumAdd.h</p>
  <pre><code>//
  // Created by sodino on 2021/7/18.
  //
  
  #ifndef ANDROIDDEMO_NUMADD_H
  #define ANDROIDDEMO_NUMADD_H
  
  int add(int a, int b);
  
  
  #endif //ANDROIDDEMO_NUMADD_H
  </code></pre>
  <p>myMath/NumAdd.cpp</p>
  <pre><code>//
  // Created by sodino on 2021/7/18.
  //
  
  #include &quot;NumAdd.h&quot;
  int add(int a, int b) {
      return a + b;
  }
  
  </code></pre>