views:

427

answers:

1

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.

+2  A: 

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
Glen
Excellent. That worked! Thanks a lot!