[CMake] error: multiple declaration of main

Maik Beckmann beckmann.maik at googlemail.com
Wed Jan 28 06:50:43 EST 2009


Am Mittwoch 28 Januar 2009 schrieb ankit jain:
> thanks maik fo rhte reply.
>
> but actually i have done something like this:
>
> add_library(myapp SHARED ${mypp_srcs})
>
> where myapp_srcs contain around 4 .C files.
>
> in this case how to do that
>
> Regards-
> ankit jain
>

Hm, you are a C++ newbie as well :)

main has a special meaning.  There is only one main per program.  Never put a 
main function inside a library!


Regarding linker symbol conflicts:
A function/variable/class can only be exported by exactly one source file.  
I.e.
source1.cpp:
  void foo() { }
source2.cpp:
  void foo() { }
crate shared library:
  g++ source1.cpp source2.cpp -shared -fPIC
results in
{{{
/tmp/ccorPWwe.o: In function `foo()':
source2.cpp:(.text+0x0): multiple definition of `foo()'
/tmp/ccwnvU0c.o:source1.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
}}}

The problem above is, that only one foo can be exported for later use by 
library clients, but the linker don't know which.  The solution is to stop one 
or both foo functions from being exported.  There are two ways to do this
  static void foo() { } // is private to the this source file
or
  namespace { // anonymous namespace
   /**
    Everything in an anonymous namespace is private to the current
    cpp.
    */
    void foo() { }
    // ... other declarations
  }  


HTH,
 -- Maik

PS: please don't top post, thank you!


More information about the CMake mailing list