Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking not done in Makefile

Tags:

c++

makefile

I tried to make a Makefile using files main.cpp, factorial.cpp, hello.cpp and function.h On typing 'make' on Linux command window, it shows:

g++ -c -o hello main.o factorial.o hello.o
g++: main.o: linker input file unused because linking not done
g++: factorial.o: linker input file unused because linking not done
g++: hello.o: linker input file unused because linking not done

I am making the Makefile for first time. Please give suggestions what can be the problem? The Makefile contains following code->

hello: main.o factorial.o hello.o
        g++ -c -o hello main.o factorial.o hello.o

main.o: main.cpp
        g++ -c -o main.o main.cpp

factorial.o: factorial.cpp
        g++ -c -o factorial.o factorial.cpp

hello.o: hello.cpp
        g++ -c -o hello.o hello.cpp

The individual file contents if you want to see are: 1) main.cpp

#include<iostream>
#include"functions.h"
using namespace std;
int main()
{
    print_hello();
    cout << endl;
    cout << "The factorial of 5 is " << factorial(5) << endl;
    return 0;
}

2) hello.cpp

#include<iostream>
#include "functions.h"
using namespace std;

void print_hello()
{
    cout << "Hello World!";
}

3) factorial.cpp

#include "functions.h"

int factorial(int n)
{
    if(n!=1)
    {
        return(n * factorial(n-1));
    }
    else return 1;
}

4) function.h

 void print_hello();  
 int factorial(int n);
like image 232
Jigyasa Avatar asked Aug 12 '13 05:08

Jigyasa


People also ask

What file is makefile?

The makefile is a text file that contains the recipe for building your program. It usually resides in the same directory as the sources, and it is usually called Makefile . Each one of these commands should be a separate rule in a makefile.

What is the makefile in c?

Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile.

Where should makefile be stored?

C clean: rm *.o prog3 Initial Notes * A makefile is stored in the same directory as your source code and is called 'Makefile' or 'makefile' (without the quotes of course). * To compile your program with a working makefile, type 'make * -k' on the command line or 'M-x compile' and then 'make -k' in emacs.

Why does makefile keep relinking?

In makefiles for substantial projects, the rules and lists of files are often more complicated, using multiple symbols to convey commands, options, and lists of files. In this context, “relink” merely means that make will execute the command to link objects into an executable again.


2 Answers

The -c parameter to g++ tells it not to link:

-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

You definitely don't want the -c here:

hello: main.o factorial.o hello.o
        g++ -c -o hello main.o factorial.o hello.o
like image 136
David Schwartz Avatar answered Sep 21 '22 09:09

David Schwartz


You could also use rules and patterns to make it more generic:

SRC_DIR = ./src
OBJ_DIR = ./bin/obj
BIN_DIR = ./build/bin

# List all the sources
SRCS = A.cpp B.cpp

# Define the rule to make object file from cpp
$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp
    g++ -o $@ $(INCLUDES) $(CPPFLAGS) -c $^

TARGET_BIN = $(BIN_DIR)/test

all : make_dirs $(TARGET_BIN)

$(TARGET_BIN) : $(SRCS:%.cpp=$(OBJ_DIR)/%.o)
    g++ $(LDFLAGS) -o $@ $^ $(LDLIBS)

make_dirs :
    mkdir -p $(OBJ_DIR)
    mkdir -p $(BIN_DIR)

With this approach you have several benefits:

  • Easy to use: you specify source files once, and you don't care about processing of each object file: the job is done by a single rule.

  • More maintainable: every time you need to change the compiler or linker options you do it in the single rule, not for each translation unit.

like image 23
nogard Avatar answered Sep 21 '22 09:09

nogard