Compiling JVMTI agent (using GCC, on OSX Snow Leopard)

ⅰ亾dé卋堺 提交于 2019-12-13 03:20:43

问题


I am trying to build a JVMTI agent using the g++ command on Snow Leopard and I get the following error:

$ g++ -o agent.so -I `/usr/libexec/java_home`/include agent.cpp

Undefined symbols: "_main", referenced from: start in crt1.10.6.o ld:
symbol(s) not found collect2: ld returned 1 exit status

I am a total novice when it comes to gcc and C++ programming so I have no idea what that error means. The agent itself is extremely basic:

      #include 
      #include 

      JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
      {
          std::cout <<"Loading aspect..." <<std::endl;
          return JNI_OK;
      }

Any help with the message would be greatly appreciated.


回答1:


The command line options you've supplied to g++ are telling it that you're trying to build an executable, not a shared library. g++ is complaining that you haven't defined a main function, as every executable requires one.

Compile your shared library with the -c flag so that g++ knows to build a library, i.e. compile and assemble your code, but don't try to link it into an executable file.

g++ -c -o agent.so -I `/usr/libexec/java_home`/include agent.cpp


来源:https://stackoverflow.com/questions/1420602/compiling-jvmti-agent-using-gcc-on-osx-snow-leopard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!