-1

I´m using Eclipse 3.8.1 on Ubuntu 14.02 with 2 projects for the first time. I´m coming from c# world so that can be an Eclipse error or a C++ concept error.

testmonitor: A sample C++ project. Code:

#include <iostream>

using namespace std;

int main() {

     cout << "Test program" << endl;
     log_access::test();

     return 0;
}

log_access is a shared library: log_access.cpp

#include <iostream>

namespace log_access {

    void test()
    {
        std::cout << "It worked!!!" << std::endl;
    }

}

I´m trying to build a shared library and link it to the main project. I went to Project -> Properties -> Project References and clicked on the project (shared lib) I want to reference.

Not worked....

Then I went to Project -> Properties -> C/C++ General -> Paths and Symbols -> References Tab and clicked on the project (shared lib) I want to reference.

Not worked...

Currently I´m getting the following error:

Invoking: GCC C++ Compiler
g++ -std:c++0x -I"home/projects/dev/sample/workspace/log_access" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testproject.d" -MT"src/testmonitor.d" -o "src/testmonitor.o" "../src/testmonitor.cpp"
../src/testmonitor.cpp: In function 'int main()':
../src/testmonitor.cpp:34.3: error: 'log_access' has not been declared
   log_access:test();
   ^
make: *** [src/testmonitor.o] Error 1

13:56:39 Build Finished (took 1s.246ms)

Obs: The log_access compiles fine...

I appreciate very much some help on that...

Mendes
  • 17,489
  • 35
  • 150
  • 263

1 Answers1

1

You'll need to include your definition of log_access::test in your main file via

#include "log_access.h"

Assuming you have a header file named log_access (you shouldn't include .cpp files; use them for implementing methods declared within the header file. See here for why).

Community
  • 1
  • 1
aQuigs
  • 347
  • 2
  • 7
  • Apart from the cosmetics (I typed the code from my Linux VM - not copied/pasted) - already corrected in the post, it seens that the problem is the absent of the header file... So, seens that for every piece of code in C++ I need a header file, even on function/namespace only... – Mendes Apr 21 '15 at 23:23
  • @Mendez not necessarily. As an oversimplification, placing declarations in header files essentially allows other files to have access to classes, methods, etc. declared by including the header file. In general, most classes/functions you write should be split between a header and source file but there are quite a few cases where this is not ideal – aQuigs Apr 21 '15 at 23:35