Xcode工程中のC++ファイルを解決する.cppファイルの相対パスを取得する問題
3651 ワード
解決策は次のとおりです.
具体的な実装ファイルは以下の通りです.
iOSエンジニアリングでは、cppファイル取得リソースファイルのパスアドレスは:
参考資料:Relative Paths Not Working in Xcode C++
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
// ----------------------------------------------------------------------------
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);
chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------
具体的な実装ファイルは以下の通りです.
LoadFile.hpp
#ifndef LoadFile_hpp
#define LoadFile_hpp
#ifdef __cplusplus
extern"C" {
#endif
#include
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
void loadFile();
#ifdef __cplusplus
}
#endif
#endif /* LoadFile_hpp */
LoadFile.cpp
#include "LoadFile.hpp"
#include
#include
#include
using namespace std;
void loadFile()
{
printf("%s, %d
", __func__, __LINE__);
ifstream inputFile;
string countries;
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)) {
// error!
}
CFRelease(resourcesURL);
chdir(path);
std::string sourcePath(path);
#if TARGET_OS_IPHONE || TARGET_OS_TV
// /var/containers/Bundle/Application/8A7E3671-5BE7-4C02-867B-58C53A391397/iOSTest.app/model.bundle/load.bin
std::cout << "Current Path: " << sourcePath << std::endl;
std::cout << "Current Path: " << (sourcePath + "/model.bundle/load.bin") << std::endl;
#elif TARGET_OS_MAC
// /Users/xxxxxx/Library/Developer/Xcode/DerivedData/MacTest-fnhmrwbqmbccbxhjywjehgdoqfib/Build/Products/Debug/MacTest.app/Contents/Resources/model.bundle/load.bin
std::cout << "Current Path: " << sourcePath << std::endl;
std::cout << "Current Path: " << (sourcePath + "/model.bundle/load.bin") << std::endl;
#endif
#endif
inputFile.open(sourcePath + "/load.bin"); // The name of the file you set up.
while(inputFile >> countries) {
cout << countries << endl;
}
inputFile.close();
}
iOSエンジニアリングでは、cppファイル取得リソースファイルのパスアドレスは:
/var/containers/Bundle/Application/8A7E3671-5BE7-4C02-867B-58C53A391397/iOSTest.app/model.bundle/load.bin
MacOSエンジニアリング中、.cppファイル取得リソースファイルのパスアドレスは:/Users/xxxxxx/Library/Developer/Xcode/DerivedData/MacTest-fnhmrwbqmbccbxhjywjehgdoqfib/Build/Products/Debug/MacTest.app/Contents/Resources/model.bundle/load.bin
sourcePath + /model.bundle/load.bin , sourcePath , load.bin 。 , "TARGET_OS_IPHONE || TARGET_OS_TV", "TARGET_OS_MAC" iOS Mac 。
参考資料:Relative Paths Not Working in Xcode C++