Я пытаюсь создать минимальный проект с использованием Cmake на MacOS, который может быть связан с файлами JPG. Цель состоит в том, чтобы показать, что файл JPG, предоставленный как аргумент, может быть открыт. /> Это то, что я сделал: < /p>
cmakelists.txt
cmake_minimum_required(VERSION 3.10)
project(MyApp)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_OSX_ARCHITECTURES "arm64")
enable_language(OBJCXX)
set(CMAKE_OBJCXX_STANDARD 17)
find_package(OpenGL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
link_directories(${GLFW_LIBRARY_DIRS})
add_executable(MyApp MACOSX_BUNDLE main.mm)
target_link_libraries(MyApp
glfw
${OPENGL_LIBRARIES}
"-framework Cocoa"
"-framework AppKit"
"-framework OpenGL"
)
set_target_properties(MyApp PROPERTIES
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/Info.plist"
)
info.plist
CFBundleDocumentTypes
CFBundleTypeName
JPEG Image
CFBundleTypeRole
Editor
LSHandlerRank
Owner
LSItemContentTypes
public.jpeg
CFBundleTypeName
AnyFile
CFBundleTypeRole
Editor
LSHandlerRank
Owner
LSItemContentTypes
public.data
main.mm
#ifdef __APPLE__
#import
#include
// Forward declare the function so the Objective-C code can call it
extern "C" void handleOpenFile(const char* path);
@interface MyAppDelegate : NSObject
@property (strong) NSWindow* dummyWindow;
@end
@implementation MyAppDelegate
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
NSLog(@"[macOS] application:openFile: %@", filename);
if (!self.dummyWindow) {
self.dummyWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 1, 1)
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:NO];
[self.dummyWindow orderFrontRegardless];
}
// Now you can call handleOpenFile safely
handleOpenFile(filename.UTF8String);
return YES;
}
@end
__attribute__((constructor))
static void setupAppDelegate() {
static bool initialized = false;
if (initialized) return;
initialized = true;
[NSApplication sharedApplication];
static MyAppDelegate* delegate = [[MyAppDelegate alloc] init];
[NSApp setDelegate:delegate];
}
#endif // __APPLE__
#include
#include
#include
#include
#include
#include
#include
static std::mutex logMutex;
void logMessage(const std::string& msg) {
std::lock_guard lock(logMutex);
const std::string logPath = "/tmp/open_files.log";
std::ofstream logFile(logPath, std::ios::app);
if (!logFile.is_open()) {
std::cerr
Подробнее здесь: https://stackoverflow.com/questions/797 ... app-in-mac