simple makefile faults

Asked by Chris

Hello Ubuntu support,

I am testing the make function on Ubuntu Jammy:
-------------
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
------------------

I have created 3 files in the same directory:

A) hello.cpp
#include "func.h"

int main() {
    func(20);
    func(100);

    return 0;
}

B) func.cpp
#include "func.h"

void func(int i) {
    std::cout << "You passed: " << i << std::endl;
}

C) func.h
#include<iostream>

void func(int i);

I have created a makefile called hello:
-----
all: hello.exe

hello.exe: hello.o func.o
        @echo "Linking object files: hello.o and func.o"
        g++ -o hello.exe hello.o func.o
        @echo "Executable hello.exe has been created"

hello.o: hello.cpp func.h
        @echo "compiling hello.cpp"
        g++ -c hello.cpp
        @echo "hello.o has been created"

func.o: func.cpp func.h
        @echo "compiling func.cpp"
        g++ -c func.cpp
        @echo "func.o has been created"
-----

I use the makefile with 'make hello' I get this message
-----
g++ hello.cpp -o hello
/usr/bin/ld: /tmp/ccFKwfPP.o: in function `main':
hello.cpp:(.text+0xe): undefined reference to `func(int)'
/usr/bin/ld: hello.cpp:(.text+0x18): undefined reference to `func(int)'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: hello] Error 1
-----

I have checked everything plus looked on the web for makefile tutorials. I can't understand why the makefile won't work.

Do you have any suggestions?

Best regards
Chris

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu cpprest Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

I do not see a definition for "hello" in your makefile, only for "hello.exe".
I assume that this is the reason that make tries the default which is just compiling and linking hello.cpp without anything else.

In my opinion this has nothing to do with Ubuntu, and question like this should probably better be asked in a C++-developers forum.

Can you help with this problem?

Provide an answer of your own, or ask Chris for more information if necessary.

To post a message you must log in.