vendredi 8 janvier 2021

How to merge three files with makefile? [duplicate]

I have two functions like function a1.cpp and b1.cpp they are separated files from main.cpp here is my a1.cpp:

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

float A(float x, float y, int z) {
    
    float A = pow(y, cbrt(x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    return A;
}

And here is my b1.cpp

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

float B(float x, float y, int z) {
    
    float B =  y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    return B;
}

My main function looks like :

#include <math.h>
#include <iostream>
#include "a1.cpp"
#include "b1.cpp"

using namespace std;



int main()
{
    float x, y;
    int   z;
    x = -0.62;
    y = 0.82;
    z = 25;

    

    float a = A(x, y, z);
    float b = B(x, y, z);
    cout << "your answer for A = " << a << endl;
    cout << "your answer for B = " << b << endl;
}

I prepared a makefile but I am completely a beginner :

CC = g++
CFLAGS = -Wall -g
  
main: main.o a1.o b1.o
    $(CC) $(CFLAGS) -o main main.o a1.o b1.o
 

 
main.o: main.cpp a1.cpp b1.cpp
    $(CC) $(CFLAGS) -c main.cpp


a1.o: a1.cpp
 
b1.o: b1.cpp

and here is my error output:

c1r10p3% make
g++ -Wall -g -c main.cpp
c++    -c -o a1.o a1.cpp
c++    -c -o b1.o b1.cpp
g++ -Wall -g -o main main.o a1.o b1.o
duplicate symbol __Z1Affi in:
    main.o
    a1.o
duplicate symbol __Z1Bffi in:
    main.o
    b1.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

When I checked the error it says I have duplicated symbols but I can't see any can you help me with that please?

Aucun commentaire:

Enregistrer un commentaire