链接目标 c++

发布于 2024-08-16 20:15:39 字数 1030 浏览 11 评论 0 原文

我试图找出为什么当我将 main.m 文件转换为 main.mm 文件时,它不再正确链接。

我已将问题简化为以下示例代码:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main( int argc, const char ** argv ) {
return NSApplicationMain( argc, argv);
}

我正在使用 gnustep 和 linux。我输入以下命令,一切按预期工作:

g++ -g -c main.m -I/usr/GNUstep/Local/Library/Headers -I/usr/GNUstep/System/Library/Heads

g++ -g -o test main .o -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lgnustep-gui

现在,如果我将 main.m 重命名为 main.mm 并使用这两个命令(相同,除了 main.m 现在是 main.mm):

g++ -g -c main.mm -I/usr/GNUstep/Local/Library/Headers -I/usr/GNUstep/System/Library/Heads

g++ -g -o test main.o -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lgnustep-gui

我收到以下错误: main.mm:7:对“NSApplicationMain(int, char const**)”的未定义引用

有人可以找到我做错了什么吗?我不明白为什么它现在无法链接。

我正在尝试将一些 C++ 类添加到目标 C 程序中,但这阻止了我继续。

感谢您提供的任何帮助。

I am trying to figure out why when I convert my main.m file to a main.mm file, it no longer will link properly.

I have reduces the problem to the following example code:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main( int argc, const char ** argv ) {
return NSApplicationMain( argc, argv);
}

I am using gnustep and linux. I enter the following commands and everything works as expected:

g++ -g -c main.m -I/usr/GNUstep/Local/Library/Headers -I/usr/GNUstep/System/Library/Headers

g++ -g -o test main.o -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lgnustep-gui

Now if I rename main.m to main.mm and use these two commands ( same exept main.m now main.mm):

g++ -g -c main.mm -I/usr/GNUstep/Local/Library/Headers -I/usr/GNUstep/System/Library/Headers

g++ -g -o test main.o -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lgnustep-gui

I get the following error:
main.mm:7: undefined reference to `NSApplicationMain(int, char const**)'

Can someone please find what I am doing wrong? I do not see why it is now failing to link.

I am trying to add some C++ classes to an objective c program and this is preventing me from continuing.

Thank you for any help you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-08-23 20:15:39

问题是,当您将其编译为 C++ 时,编译器破坏了符号 >NSApplicationMain,所以它找不到它,因为它正在寻找类似 __Z17NSApplicationMainiPPKc 的内容。您可以使用 nm 程序(来自 binutils)来查看目标文件引用了哪些符号:

$ # When compiled as Objective-C:
$ nm main.o | grep NSApplicationMain
                 U NSApplicationMain
$ # When compiled as Objective-C++:
$ nm main.o | grep NSApplicationMain
                 U _Z17NSApplicationMainiPPKc

为了避免此问题,需要使用 extern "C" 修饰符为了告诉编译器不要破坏名称。查看声明 NSApplicationMain 的头文件 ,我看到:

APPKIT_EXPORT int
NSApplicationMain(int argc, const char **argv);

唉,APPKIT_EXPORT 被定义为可以是 extern__declspec(dllexport)extern __declspec(dllexport) 之一,或者 中不包含任何内容;。由于它也用于全局变量声明,因此我们无法通过将其重新定义为 extern "C" 来解决这个问题(无论如何,这将是非常hacky和kludgy的)。 AppKit 头文件似乎根本不包含任何 extern "C" 声明,尽管我确实在 Foundation/GNUStepBase/ 下的各种头文件中看到它们

那么你能做什么呢?解决方案是用 extern "C" 包装您的包含文件:

extern "C"
{
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
}

int main( int argc, const char ** argv ) {
  return NSApplicationMain( argc, argv);
}

这将为这些头文件中定义的函数提供正确的链接,并且一切都会成功。但您不必这样做——我会向 GNUstep 提交错误报告,告诉他们将正确的 extern "C" 声明添加到其头文件中。

The problem is that when you compile it as C++, the compiler mangles the name of the symbol NSApplicationMain, so it can't find it, since it's looking for something like __Z17NSApplicationMainiPPKc. You can use the nm program (from binutils) to see what symbols the object files are referencing:

$ # When compiled as Objective-C:
$ nm main.o | grep NSApplicationMain
                 U NSApplicationMain
$ # When compiled as Objective-C++:
$ nm main.o | grep NSApplicationMain
                 U _Z17NSApplicationMainiPPKc

In order to avoid this problem, C functions need to be declared with an extern "C" modifier in order to tell the compiler not to mangle the name. Looking into <AppKit/NSApplication.h>, the header file where NSApplicationMain is declared, I see this:

APPKIT_EXPORT int
NSApplicationMain(int argc, const char **argv);

Alas, APPKIT_EXPORT is defined to be one of extern, __declspec(dllexport), extern __declspec(dllexport), or nothing in <AppKit/AppKitDefines.h>. Since it's also used for global variable declarations, we can't get around this by redefining it to extern "C" (which would be extremely hacky and kludgy anyways). The AppKit header files do not seem to contain any extern "C" declarations at all, although I do see them in various header files under Foundation/ and GNUStepBase/.

So what can you do? The solution is to wrap your includes with an extern "C":

extern "C"
{
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
}

int main( int argc, const char ** argv ) {
  return NSApplicationMain( argc, argv);
}

This will give the functions defined in those header files the proper linkage, and it will all work out. But you shouldn't have to do this -- I would file a bug report with GNUstep, telling them to add proper extern "C" declarations to their header files.

过潦 2024-08-23 20:15:39

问题是 c++ 编译器通常使用名称重载来在链接阶段启用函数重载。

C++ 定义了一个 extern "C" 指令,强制其对函数使用 C 兼容名称。

您可以在 C++ 文件中使用它,如下所示:-

  // this makes func use a C compatible linkage
  extern "C" void func(int a)
  {

在 C 和 C++ 包含的头文件中,有必要保护 C 的 extern“C” 声明,而 C 编译器不理解它。

#ifndef EXTERN_C
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#endif
// Use it like this to declare a Function with C linkage
EXTERN_C void func(int a);
// If you have a lot of functions and declarations that need to be C compatible
#ifdef __cplusplus
extern "C" {
#endif
//functions with C linkage
void func(int a);
...
#ifdef __cplusplus
}
#endif

现在,这对您的问题有何帮助?好吧,main.mm 意味着 Foundation.h 和 AppKit.h 文件正在被编译为 C++。我无法猜测为什么苹果没有使用外部“C”指令来保护 NSApplicationMain,但它显然没有受到保护。

一个简单但残酷的修复方法是更改​​您的#imports,如下所示:

extern "C" {
// All declarations inside this block will use C linkage
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
}

int main( int argc, const char ** argv ) {
  return NSApplicationMain( argc, argv);
}

The problem is the name mangling c++ compilers typically use to enable function overloading at the link stage.

C++ defines a extern "C" directive that forces it to use a C compatible name for a function.

You can use it in a C++ file like so :-

  // this makes func use a C compatible linkage
  extern "C" void func(int a)
  {

In a header file that is included by C and C++ it is necessary to protect the extern "C" declaration from C, and the C compiler does not under stand it.

#ifndef EXTERN_C
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#endif
// Use it like this to declare a Function with C linkage
EXTERN_C void func(int a);
// If you have a lot of functions and declarations that need to be C compatible
#ifdef __cplusplus
extern "C" {
#endif
//functions with C linkage
void func(int a);
...
#ifdef __cplusplus
}
#endif

Now, how does this help your problem? Well, main.mm means the Foundation.h and AppKit.h files are being compiled as C++. Why Apple have not protected NSApplicationMain with a extern "C" directive I cannot guess, but it clearly isn't guarded.

A simple, if brutal, fix would be to alter your #imports like follows:

extern "C" {
// All declarations inside this block will use C linkage
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
}

int main( int argc, const char ** argv ) {
  return NSApplicationMain( argc, argv);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文