2

I am new to c++ programming and know the basics of c++. I am trying to link three files, but i am unable to get the output.I am studying the book c++ cookbook right now

Suppose we have the files a.hpp a.cpp, b.hpp b.cpp,c.hpp c.cpp with the following code:

a.hpp

#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED
void a();
#endif

a.cpp

#include "a.hpp"
#include <iostream>
void a()
{
    std::cout<<"a \n ";
}

b.hpp

#ifndef B_HPP_INCLUDED
#define B_HPP_INCLUDED
void b();
#endif

b.cpp

#include "b.hpp"
#include <iostream>
void b()
{
    std::cout<<"b \n ";
}

c.hpp

#ifndef C_HPP_INCLUDED
#define C_HPP_INCLUDED
void c();
#endif

c.cpp

#include "a.hpp"
#include "b.hpp"
#include "c.hpp"
void c()
{
    a();
    b();

}

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

I have created all the files in one folder and commands i used to compile and link them are

$:g++ -c -Wall a.cpp b.cpp c.cpp
$:g++ -o -Wall a.o b.o c.o
$:./a.out

I was expecting the outout

a
b

but there was no output at all.Request you all to help me out with this.

Bo.
  • 2,547
  • 3
  • 24
  • 36
Sid
  • 481
  • 1
  • 4
  • 20
  • The first paragraph mentions `ab.h` and `ab.c`, but those are not used. Are the build commands correct? Ignoring those two files, it all looks correct and should work as you expect. – wallyk Jun 22 '12 at 14:50

2 Answers2

3

Remove the -o from your g++ command line.

You are currently telling g++ to link your objects together to a file called -Wall. So the alternative solution would be to call ./-Wall instead of ./a.out.

Due to the spaces in your strings (" \n " vs. "\n"), you'll still not be getting the exact expected output. Also you will probably want to replace:

std::cout << "something \n";

with

std::cout << "something" << std::endl;

if you want to have the data on screen immediately (flushing). See also C++: “std::endl” vs “\n”.

Community
  • 1
  • 1
Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
  • Oddly enough, the top answers in the linked question contradict what you're stating... – stefaanv Jun 22 '12 at 15:00
  • Can you add a quote? From what I read, they do not contradict, at least the accepted one doesn't. It just says that if you want to flush your output, use `std::endl`. On command line output, I usually do that to avoid loss of (debugging) data in the case of a crash or anything. – Jonas Schäfer Jun 22 '12 at 15:10
  • It contradicts your claim that `endl` helps portability. That claim is spoiling an otherwise good answer. – Jonathan Wakely Jun 22 '12 at 15:44
  • Humm, I thought so. However, reading over the linked answer again showed me that I'm wrong, going to edit immediately. – Jonas Schäfer Jun 22 '12 at 15:55
  • @JonasWielicki Thank you :) when i typed in ./-Wall I was able to get the output.But i actually used it so that the compiler can show warnings if there are any.Could you please let me know the correct usage of -Wall? Thanks a ton :) – Sid Jun 22 '12 at 23:09
  • `-Wall` is okay. But `-o` expects a file name as argument, so you'd want to type for example `g++ -Wall -o foo a.o b.o c.o` so that your executable is then called `foo`, runnable with `./foo`. – Jonas Schäfer Jun 23 '12 at 10:56
0
$:g++ a.o b.o c.o -o your_output_program_name -Wall

try using this command. You will certainly get all things working correctly