0

hi can any one help me with this code. i'm new to c++

#include <stdio.h>
#include<iostream>
using namespace std;

namespace manish
{
  double mean(double a, double b);
}

double mean(double a, double b)
{
  return (a+b) / 2;
}

i want to make a static libray for this i use the command g++ calc_mean.cpp -o mean.o it give me following error

/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

can anyone tell me what i'm doing wrong? or how to make a namespace for a function so that it can be used in another place by using its name space

2
  • 3
    Why have you declared mean in the manish namespace, but defined it anonymously. Also, this question has nothing to do with namespaces, but how to compile a static library using GCC. Mar 17, 2011 at 7:30
  • have a look here to make a static library to calculate the mean: adp-gmbh.ch/cpp/gcc/create_lib.html
    – stefaanv
    Mar 17, 2011 at 7:36

4 Answers 4

9

This is nothing to do with namespaces. If you want to compile and not link you have to provide the -c option to gcc:

g++ -c calc_mean.cpp -o mean.o

The error you are getting is because g++ is trying to create an executable program (you haven't told it otherwise) for which you need to define the standard entry point: main.

There is nothing inherently wrong with declaring a mean in one namespace and defining another mean at global namespace scope, although in this instance the first declaration is not used so doesn't have any effect.

To make a static library from the resulting object file you need to use the archive utility (ar). E.g.:

ar rcs libmean.a mean.o
1

Build with g++ -c option to tell gcc to not invoke the linker, since you are building library and have nothing to link.

0

You are using -o which means the compiler expects the files to create a complete program. However, a complete program must contain a main() function. You should use:

g++ -c calc_mean.cpp

If you do not have a main function.

0

main is the function that will called when your program loads. You're telling compiler to create a program that when run will have a main function.

add this to your code

void main(){
  printf("My program has started");
}

Also you have declared a function called manish::mean (mean function in the manish namespace) but you have provided the code for the function mean in the global namespace ::mean

you should try something like this:

#include <stdio.h>
#include<iostream>
using namespace std;
namespace manish
{
  double mean(double a, double b);

  double mean(double a, double b)
  {
    return (a+b) / 2;
  }
}

void main(){
  double result = manish::mean(2.0, 4.0);
  printf("My program has started - the result is %f", result);
}
1
  • 1
    void main() is incorrect. main must have a return type of int.
    – CB Bailey
    Mar 17, 2011 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.