Hi! To Everyone! Could you tell me how I can use GCC with that file comunicatoin, because : g++ main.cpp program1.cpp -o program1.exe - not valid - It didn't saw program1.h and all declaration in him.

I had the task : Write a program that prints the numbers from 1 to 100. But for multiples of three print “Global” instead of the number and for the multiples of five print “Logic”. For numbers which are multiples of both three and five print “GlobalLogic"

//program1.h
#ifndef PROGRAM1_H
#define PROGRAM1_H 1
#include <iostream>
#define HUNDREDPLUS1 101
#define BEGIN1 1
#define K3 3
#define K5 5

    using std::cout;
    using std::endl;
    void Multiples_3_5();

#endif //PROGRAM1_H

//main.cpp
#include"program1.h"

int main (int argc, const char * argv )
{
  Multiples_3_5();

  return 0;
}// main

//program.cpp
/**
 * @file    program1.cpp
 * @brief   A source file for program1
 *
 *
 * Copyright 2014 by Nikolas9896,
 *
 * This software is the confidential and proprietary information
 * of Nikolas9896 ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Nikolas9896.
 **/


void Multiples_3_5( )
{
    unsigned short it = BEGIN1;
    while(  it < HUNDREDPLUS1 )
    {
        if( ( !( it % K3 ) && !( it % K5 ) )  )
        {
            cout << "GlobalLogic"<< it << endl;
        }
        else if( !( it % K3 ) )
        {
            cout << "Global" << it << endl;
        }

        else if( !( it % K5 ) )
        {
            cout << "Logic" << it << endl;
        }

        else
        {
            cout << it << endl;
        }

    ++it;
    }
}

Dani AI

Generated

As discovered, the real problem was that the translation unit using the macros/declarations did not include the header that defined them. Any .cpp that relies on symbols or macros from a header must explicitly #include that header; missing that include often looks like "file not found" or "identifier not declared" depending on whether the compiler can't see the header or the preprocessor can't see the macros.

Common causes and quick diagnostics

  • Typos or wrong filename/case (Linux is case-sensitive).
  • Working directory vs. source directory mismatch (out-of-source builds).
  • Header in a non-standard folder (requires an include path).
  • Invisible characters or wrong extension (.h vs .hpp).
  • To inspect what the preprocessor actually finds, run the compiler in preprocessing/diagnostic mode (show include trace or dependency list) and compile units separately:
g++ -E -H main.cpp       # show include trace
g++ -c program1.cpp      # compile program1.cpp alone to reveal unit-specific errors
g++ main.o program1.o -o program1

Notes on advice already posted

  • was right to ask for the exact error — the message usually points to the root cause.
  • 's tip about adding include paths is correct when headers live outside the source tree.
  • 's single-file example is a quick way to validate the printing logic; for real projects separate compilation is preferable.

Style and maintenance tips

  • Avoid using declarations in headers; they leak names into every file that includes the header. Put using in .cpp files.
  • Prefer constexpr/const over #define for typed constants.
  • Prefer '\n' to std::endl unless an explicit flush is required.
  • Keep a minimal Makefile or simple build rule to compile units with -c and then link — that makes it clear which unit fails and why.

A short, modern header pattern (illustrative) follows:

#ifndef PROGRAM1_H
#define PROGRAM1_H

#include <iostream>
constexpr unsigned BEGIN_V = 1u;
constexpr unsigned END_EXCL = 101u;

void Multiples3_5();

#endif // PROGRAM1_H

Recommended Answers

All 5 Replies

If the compiler (actually pre-processor in this case) cannot find a file it is looking for, then you have not told it where to look, or put the file in the wrong place, or misnamed the file. What is the exact error message?

By the way, posting your code on Daniweb breaks this part of your licence:

You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Nikolas9896.

as part of Daniweb's TOS:

Posts contributed to the community immediately become the property of DaniWeb upon submission.

Anywho, try compiling the "program1" unit and "main" unit seperately. Which unit cannot find the file?

If we had the error message like Moschops said we would know this, but sometimes it's better practice to break down the compilation so you can see which stage the error happens at and you don't need to recompile the project from the beginning. This can be automated with something like a makefile.

This is such a small problem that it seems more appropriate to just do it all in one short file:

Note: the print logic flow can be made clear here by using two bool 'flags' to track what has already been printed:

for( int i = 1; i < 101; ++i )
{
    bool global = false,
         logic = false;

    if( i % 3 == 0 ) { global = true; cout << "global"; }
    if( i % 5 == 0 ) { logic = true; cout << "logic"; }

    if( !(global || logic) ) cout << i;

    cout << endl;
}

I the absence of other information, I would recommend trying the -I option with the path to the header files in question.

g++ -Ipath\to\include main.cpp program1.cpp -o program1.exe

However, if they are in the path already that shouldn't be necessary, so there may be more to this than that.

Thanks for all whose helped
Finally version of program :) David W - thank for idea - I update algoritm of prog but I used only one mark...

//program1.h
#ifndef PROGRAM1_H
#define PROGRAM1_H 1
#include <iostream>
#define HUNDREDPLUS1 101 // High limit
#define BEGIN1 1 // Low limit
#define M3 3 //It's checked digits multiples of 3
#define M5 5 //It's checked digits multiples of 5

    using std::cout;
    using std::endl;
    void Multiples_3_5();

#endif//PROGRAM1_H

//program1.cpp
#include"program1.h" // THE ERROE WAS FOUND !)

void Multiples_3_5( )
{
    bool is_printed;
    for( unsigned short it = BEGIN1; it < HUNDREDPLUS1; ++it )
    {
        is_printed = false;

        if( !( it % M3 ) )
        {
            is_printed = true;
            cout << "Global";
        }
        if( !( it % M5 ) )
        {
            is_printed = true;
            cout << "Logic";
        }

        if( !( is_printed ) )
        {
            cout << it;
        }

        cout << endl;

    }
}

//main

#include"program1.h"

int main (int argc, const char * argv )
{
  Multiples_3_5();

  return 0;
}// main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.