4 Replies - 1552 Views - Last Post: 21 February 2014 - 05:59 PM Rate Topic: -----

#1 steadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-July 12

Compiling C++ Code With Static Library

Posted 20 February 2014 - 09:50 AM

Hey there,

I am trying to compile a c++ programme with a static library but I am getting a ton of unexpected errors. My programme is simple and is just:
#include <iostream>
int main()
{
   std::cout << "hi" << std::endl;
   return 1;
}



I am compiling it on the command line (MINGW G++) like this:
g++ main.cpp -o main //To create the main.o
g++ -o main.exe libPAPI.a main.o //To create the executable. LibPAPI is the library

For some unknown reason the above gives me the following compiler errors:
In function '[email protected]": multiple definition of [email protected]
In function "at exit": multiple definition of atexit
In function 'onexit": multiple definition of onexit
In function 'dllcrt.c": multiple definition of 'gcc_register_frame'
In function 'dllcrt.c": multiple definition of 'gcc_deregister_frame'
and these all seem to stem from mingw32.a

Anybody have any ideas as to what this means or where I have gone wrong?

Thanks in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: Compiling C++ Code With Static Library

#2 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Compiling C++ Code With Static Library

Posted 20 February 2014 - 09:59 AM

but having a main function in a static library is useless....sorry but i don't know about thr error.. :(
http://stackoverflow...library-project

This post has been edited by yashwanth.c.b: 20 February 2014 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Compiling C++ Code With Static Library

Posted 20 February 2014 - 10:15 AM

Where did you get your libPAPI library, that has been compiled for Windows?

Edit: Also your compile line should have a switch or two in front of that libPAPI.a entry. You may want to find and check the documentation for gcc to find out how to use an external library on the compile command line.



Jim

This post has been edited by jimblumberg: 20 February 2014 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: Compiling C++ Code With Static Library

Posted 20 February 2014 - 05:41 PM

Replying OP:

I found this Getting Started tutorial from the PAPI Library website:
http://icl.cs.utk.ed...Getting_Started

Try to follow what it says. Usually, libraries have documentation that tells you how to compile the projects that contain it; some libraries have special package-stuff that they have you put into the parameters instead of the usual methods, so documentation is always important.

Also to note, g++ main.cpp -o main does not create main.o; it creates an executable named main. What's interesting is that the second line g++ -o main.exe libPAPI.a main.o doesn't complain that main.o doesn't exist.

Plus, are you compiling from Command Prompt or MinGW Shell?, because that will change how the parameters to g++/gcc are written.


EDIT: minor edit changing the wording.

This post has been edited by sungchoiok: 20 February 2014 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1269
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: Compiling C++ Code With Static Library

Posted 21 February 2014 - 05:59 PM

The library will only be linked to dependencies to the left of where libPAPI.a is declared in the command. Link the library at the end of the command.

e.g
g++ main.o -o main.exe  -L/. -lPAPI



-l is a common practice to link libraries, rather than specifying the entire name of the lib.

-L is the path of where the lib is located

This post has been edited by jjl: 21 February 2014 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1