-3

I'm attempting to write a simple program that calls a function written in a pair of Header and CPP files.

I'm doing this on a Raspberry Pi 3 Model B, and the Geany IDE v1.37.1.

Compile Command:

g++ -Wall -c "%f" -c test.cpp

Build Command:

g++ -Wall -o "%e" "%f" -o test test.cpp

main.cpp:

#include "test.h"

int main()
{
    test_function();
    
    return 0;
}

test.h:

#ifndef _test_h_
#define _test_h_

#include <iostream>

void test_function();

#endif

test.cpp:

#include "test.h"

void test_function()
{
    std::cout << "hello world";
}

The code above compiles & builds fine, however attempting to run it yields the following error:

./main: not found

(program exited with code: 127)

Perhaps I am messing something up with the Compile & Build Commands?

Thank you for reading my post, any guidance is apprecaited!

Runsva
  • 365
  • 1
  • 7
  • 1
    Your two build commands don't make sense. If you do a two-stage build with separate compilation and linking steps only the first command should be taking the `.cpp` file as input and there should be one command for each `.cpp` file. I don't now what the `%` placeholders refer to in your IDE, but in the second command you should be passing the object files (`.o`) files for linking. You also give the `-o` option twice which doesn't make sense. Repeating `-c` in the first command is also pointless. – user17732522 Nov 28 '22 at 01:04
  • 1
    If you do compilation and linking in one step then there should only be the second command and it should take all `.cpp` files as input. (And there should be only one `-o` option). – user17732522 Nov 28 '22 at 01:07
  • You're right, these compilation commands I was using appear to not be working correctly. I'm a bit confused on how I'm supposed to write them. I have 2 Command options, one for `Compile` and one for the `Build`. The differing argument between the two appear to be `-c` and `-o`. I notice that the `Build` command takes two arguments, one for the name of the program, and one for the supposed input files. – Runsva Nov 28 '22 at 03:27
  • If I put all the cpp files in the `Compile` command (`g++ -Wall -c "test.cpp"`), I get an error for multiple redefinitions of the function in `test.cpp`. Is there a way to go around that? – Runsva Nov 28 '22 at 03:29
  • What are the placeholders like `"%f"` representing? Maybe they are already the file names and it is wrong to add any manually at all? This should be explained somewhere in the documentation for your IDE. – user17732522 Nov 28 '22 at 06:13

1 Answers1

4

Notice the compile command:

-o test

This means that the output binary will be test, so you can execute the application in your terminal or shell via ./test.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
lorro
  • 10,687
  • 23
  • 36
  • What does it mean "call it via ./test"? You mean rename my "main" function to "test"? – Runsva Nov 28 '22 at 00:58
  • 3
    @Runsva No, function name is fine. 'attempting to run' - how do you attempt to run it? Normally, you'd run it as `./test`. Alternatively, if the IDE is running it for you, you might use `-o main` instead of `-o test`. – lorro Nov 28 '22 at 01:00
  • ***What does it mean "call it via ./test"? You mean rename my "main" function to "test"?*** No its not the name of the function called. Its the name of the executable program. Since this is confusing to you it appears that you need to learn a little more about your terminal and how to run programs from the terminal in your OS. Here is a link: [https://magpi.raspberrypi.com/articles/terminal-help](https://magpi.raspberrypi.com/articles/terminal-help) – drescherjm Nov 28 '22 at 02:01
  • Also here: [https://www.linuxfordevices.com/tutorials/linux/build-execute-cpp-code-in-terminal](https://www.linuxfordevices.com/tutorials/linux/build-execute-cpp-code-in-terminal) – drescherjm Nov 28 '22 at 02:06
  • Ok thanks for your comments. It appears that my problem is actually the lack of cpp files in the `Compile` command. How would I properly include my cpp files, so I don't get multiple redefinitions of the function? – Runsva Nov 28 '22 at 03:32
  • 1
    You probably want to ask a brand new question about the new problem. That is a different topic than this one which is answered. For that new question you will have to add the exact details of how you built your code. – drescherjm Nov 28 '22 at 04:47
  • Ok got it, thanks for the feedback! This answer solved the problem of the incorrect output executable being called. – Runsva Nov 29 '22 at 06:52