'class" has a previous declaration here - c++

I cannot for the life of me figure out what's wrong.
My makefile:
all: main.o rsa.o
g++ -Wall -o main bin/main.o bin/rsa.o -lcrypto
main.o: src/main.cpp inc/rsa.h
g++ -Wall -c src/main.cpp -o bin/main.o -I inc
rsa.o: src/rsa.cpp inc/rsa.h
g++ -Wall -c src/rsa.cpp -o bin/rsa.o -I inc
My main class:
#include <iostream>
#include <stdio.h>
#include "rsa.h"
using namespace std;
int main()
{
//RSA rsa;
return 0;
}
My .cpp:
#include "rsa.h"
#include <iostream>
using namespace std;
RSA::RSA(){}
My .h:
#ifndef RSA_H
#define RSA_H
class RSA
{
RSA();
};
#endif
I'm getting the following error:
In file included from src/main.cpp:7:0:
inc/rsa.h:7:7: error: using typedef-name ‘RSA’ after ‘class’
/usr/include/openssl/ossl_typ.h:140:23: error: ‘RSA’ has a previous declaration here
I feel like I've tried everything but I'm stuck. Any ideas?

/usr/include/openssl/ossl_typ.h:140:23: error: ‘RSA’ has a previous declaration here
From the error message it seems that You have a symbol name clash with another class named RSA defined inside OpenSSL.
There are two ways to overcome the problem:
Change your class name or
Wrap up in a namespace if you want to keep the same name.

Your compiler found a typedef for RSA in the ossl_typ.h file, which is indirectly #included when you compile your program. I can think of at least three solutions:
Change your class name to something else.
Put your class in a namespace.
Figure out why the OpenSSL header is included in your build. After looking around, I found this Q&A which says that gcc -w -H <file> will show you the files which are #included. From there you might be able to remove the dependency on the OpenSSL headers.

Related

g++ as of 4.7 ignores the case of names inside braced #include

Consider the following code where test.h attempts to include the file "signal.h" using braced include.
Signal.h:
#define xyz 5
int testvar;
test.h:
#if !defined (_C1)
#define _C1
#include <signal.h>
int testvar2;
#endif
test.cpp:
#include "test.h"
If you compile with:
g++ -dD -E test.cpp -o C1.i
All is fine and the system signal.h is pre-pended to test.cpp. However, if you compile with:
g++ -dD -E -I. test.cpp -o C1.i
The local Signal.h is pre-pended, even if it has a different case.
Is this intended behavior? This is affecting gcc-4.7, gcc-5.4 on an Ubuntu.
Thank you
You included the current directory as part of the search path (what #include <> looks at), that is what the -I. does. It should be fully expected to get a signal.h from the local directory first in this case. As for file name case that would dpend on your system, if the system is case-sensitive then #include should be as well, if it is not then well...
I must say that -I. is very unusual as #include "" already starts there.

Multi-file C++ compilation

(hopefully) quick question that I can't find the answer to:
I have been given a brief assignment in C++. We are to write a 3-file program. There will be a function file, a header file, and a driver file. Here's what I've got so far:
Header (test.h):
#include <iostream>
using namespace std;
#ifndef TEST_H
#define TEST_H
int foo (int bar);
#endif
Function (test.cpp):
#include <iostream>
#include "test.h"
using namespace std;
int foo (int bar){
bar++;
}
Driver (drive.cpp):
#include <iostream>
#include "test.h"
using namespace std;
int main(){
int x = foo(2);
cout << x << endl;
return x;
}
When I try to compile drive.cpp, I get the following error:
drive.cpp:(.text+0xe): undefined reference to `foo(int)'
So...what am I doing wrong?
For a small project like this, simply compile all .cpp files at once:
g++ main.cpp driver.cpp
For a larger project, you separate the compile and link steps:
compile:
g++ -c main.cpp -o main.o
g++ -c driver.cpp -o driver.o
link:
g++ main.o driver.o
Or rather, you'd have a makefile or IDE do this for you.
In drive.cpp, instead of
#include <test.h>
make it
#include "test.h"
This is the variant of #include syntax that is used for header files of your own program (not system header files). When you use this version the preprocessor searches for include files in the following order:
In the same directory as the file that contains the #include statement.
In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
You need to do one of two things:
Compile all the files at once
# replace 'driver.exe' with what you want your executable called
g++ -Wall -ggdb -o driver.exe main.cpp driver.cpp
Compile all the files to object files and then link the object files:
# again, replace 'driver.exe' with what you want your executable called
g++ -Wall -ggdb -o main.o -c main.cpp
g++ -Wall -ggdb -o driver.o -c driver.cpp
g++ -Wall -ggdb -o driver.exe main.o driver.o
As a side note, you should probably change
#include <test.h>
to
#include "test.h"
and putting "using namespace std;" in a header file is going to cause you copious grief later on.
in test.cpp, change the return line to this:
return bar++;

Boost.Python can't find pyconfig.h. Where does it need to go?

I've written a very simple c++ function in main.cpp:
#include <iostream>
using namespace std;
int SomeCalculation(float x){
int decision = 0;
if (x > 1){
decision = 1;
}
return decision;
}
I'm now trying to compile this as a shared library using Boost.Python. For this I created decision.cpp:
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(decision)
{
using namespace boost::python;
def("main", main);
}
Unfortunately I get the following error:
In file included from /usr/include/boost/python/detail/prefix.hpp:13:0,
from /usr/include/boost/python/args.hpp:8,
from /usr/include/boost/python.hpp:11,
from decision.cpp:1:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: File or folder does not exist.
compilation terminated.
Since I had no clue of where this file could be I did a simple sudo find / -name pyconfig.h, which found several pyconfig.h files. So I simply copied what to me seemed the most general version of the file, to the folder in which I'm working:
cp /usr/include/python2.7/pyconfig.h /home/kram/c++/cmod/pyconfig.h
Running my compile command again (g++ -fPIC -g -ggdb -c decision.cpp -o decision.so) gives me the same error as before though.
Does anybody know how I can solve this pyconfig.h dependency?
[edit] Added pieces of code
Try command:
g++ -g -shared -fPIC -I/usr/include/python2.7 decision.cpp -lpython2.7 -lboost_python -o decision.so

G++ shows error about undefined namespace

I have 3 files to compile with G++, the main file is like this:
//main.cpp
#include "test.hpp"
int main(int argc,char** args) {
//
}
The second file is the header file:
//test.hpp
namespace shared {
class test {
//constructor
test();
};
}
The last file is the code file for test.hpp
//test.cpp
shared::test::test() {
//
}
And I compile using G++ this way:
g++ -c main.cpp test.cpp
However, G++ complains about undefined identifier 'shared' in the file 'test.cpp'. In the command line I already pass in file 'main.cpp', which includes the header file. How to fix this? I only want to have all the '#include's be in main.cpp, and no where else.
Add #include "test.hpp" at the beggining of test.cpp.
Compiler doesn't care about the order of files in the commandline. It only affects the linker.
Please also note, that the usual way of compiling multi-file projects is to compile each of them to different sub-object like so:
g++ main.cpp -o main.o
g++ test.cpp -o test.o
ld main.o test.o -o program[.exe]
This allows you to recompile only the files that really did change. If you think about it for a while, you'll find out that a .cpp file can contain many headers without a problem; however, the compilation time will increase when your headers will start to have many headers included. Forward declarations can help solve those issues, yet with your simple example simple solution will work.
You have to say #include "test.hpp" in your test.cpp file. The namespace declaration has to be known.

Compilation problem in the standard x86_64 libraries

I am having trouble compiling a program I have written. I have two different files with the same includes but only one generates the following error when compiled with g++
/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status
The files I am including in my header are as follows:
#include <google/sparse_hash_map>
using google::sparse_hash_map;
#include <ext/hash_map>
#include <math.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using std::priority_queue;
using std::stack;
using std::vector;
using __gnu_cxx::hash_map;
using __gnu_cxx::hash;
using namespace std;
Searching the internet for those two lines hasn't resulted in anything to help me. I would be very grateful for any advice. Thank you
To build two separate programs you need both source files to define main() function.
To build a single program out of two source files - first compile each file with -c options (compile only) - you will get two .o files, then link these files together. Something like this:
$ g++ -Wall -pedantic -ggdb -O -c -o module0.o module0.cpp
$ g++ -Wall -pedantic -ggdb -O -c -o module1.o module1.cpp
$ g++ -Wall -pedantic -ggdb -O -o prog module0.o module1.o
to build binary prog from two source files.
If you need to link with some library, you'll have to point compiler to it's headers with -I and to objects with -L flags, then tell the linker to actually reference the library with -l.
Hope this helps.
You need a main function and you don't have one. If you do have a main function, show more code please.
It looks like main is not defined. Do you have one defined for your second program? Can you post more details about the source body that fails to link?