C++ CMake not referencing function with .h properly - c++

SOLUTION:
Just don't be a noob and run make in the same directory as cmake
Thanks for your answers!
I'm starting with C++ programming and I'm trying to understand how to properly reference a function with .h and .cpp file. I have following files:
\\func.h
#ifndef FUNC_H
#define FUNC_H
int charout(int a, char b);
#endif
\\func.cpp
#include <iostream>
#include "func.h"
using namespace std;
int charout(int a, char b)
{
cout << a;
cout << b;
return 0;
}
\\main.cpp
#include <iostream>
#include "func.h"
using namespace std;
int main ()
{
int a; char b;
cout << "insert an integer " << endl;
cin >> a;
cout << "insert a letter " << endl;
cin >> b;
charout(a,b);
return 0;
}
I am compiling using CMake (with func.h in folder 'include') with following structure:
# Declare the version of the CMake API for forward-compatibility
cmake_minimum_required(VERSION 2.8)
# Declare the name of the CMake Project
project(manual)
# Add the directory to search for header files
include_directories(include)
# Define an executable target
add_executable(main func.cpp main.cpp)
When I try to make main.cpp I am receiving an error:
make main g++ main.cpp -o main /tmp/cctlsXUG.o: In function
main': main.cpp:(.text+0x80): undefined reference tocharout(int,
char)' collect2: error: ld returned 1 exit status : recipe
for target 'main' failed make: *** [main] Error 1
Can you please take a look and let me know where am I doing a mistake?
I will appreciate your feedback. I'm really stuck with this. Cheers!
UPDATE:
OK I managed to compile it with g++ func.cpp main.cpp - o main when I was trying to compile the same code with gcc I got errors of sort undefined reference to std::cout'
I've found out that gcc does not give an access to std of C++. Can I somehow fix my CMake files to use g++ instead of gcc? The project I have to deliver is supposed to have a CMakeLists.txt included.
Finally the last question: why does CMake works fine with function declaration and main in the same file, but not when I split the function into header, cpp and main?

Related

Using same namespace in C++ for two classes declared and defined in separate files

I am trying to declare two classes C1 and C2 in files nstest1.h and nstest2.h which are defined in files nstest1.cpp and nstest2.cpp respectively. Both the classes are defined under same namespace.
Following are the files :
//nstest1.h
namespace Mine{
class C1{
public:
void callme();
};
}
//nstest2.h
namespace Mine {
class C2 {
public:
void callme();
};
}
//nstest1.cpp
#include<iostream>
#include "nstest1.h"
using namespace std;
using namespace Mine;
void Mine::C1::callme(){
std::cout << "Please call me " << std::endl;
}
//nstest2.cpp
#include<iostream>
#include "nstest2.h"
using namespace std;
using namespace Mine;
void Mine::C2::callme(){
std::cout << "Please call me too" << std::endl ;
}
Following file tries to use this classes using namespace Mine.
//nstest.cpp
#include<iostream>
#include "nstest1.h"
#include "nstest2.h"
using namespace std;
using namespace Mine;
int main(){
Mine::C1 c1;
Mine::C2 c2;
c1.callme();
c2.callme();
return 0;
}
When I compile using command "g++ nstest.cpp", I get following error :
/tmp/cc2y4zc6.o: In function `main':
nstest.cpp:(.text+0x10): undefined reference to `Mine::C1::callme()'
nstest.cpp:(.text+0x1c): undefined reference to `Mine::C2::callme()'
collect2: error: ld returned 1 exit status
If the definitions are moved to the declaration files (nstest1.h and nstest2.h), it works fine. Not sure whats happening here. Am I missing something ?
Thanks in advance :) .
You need to include the other .cpp files when building the program.
Option 1: Compile all the files and build the executable in one command
g++ nstest.cpp nstest1.cpp nstest2.cpp -o nstest
Option 2: Compile each file separately and then build the executable after that
g++ -c nstext1.cpp
g++ -c nstest2.cpp
g++ -c nstest.cpp
g++ nstest.o nstest1.o nstext2.o -o nstest
Your problem happens at link time. Your headers are fine. But you should compile the other cpp files aswell.

How to properly include Header and Implementation Files?

I am a novice programmer in c++, and I am currently getting a compiling error
Undefined symbols for architecture x86_64
Supposedly this originates from how the header files and implementation files are included/coded.
Below is some code that generates the compiling error I am receiving
Main
//Main.cpp
#include <iostream>
#include <string>
#include "Animal.hpp"
using namespace std;
int main(){
Animal myPet;
myPet.shout();
return 0;
}
Header
//Animal.hpp
#ifndef H_Animal
#define H_Animal
using namespace std;
#include <string>
class Animal{
public:
Animal();
void shout();
private:
string roar;
};
#endif
Implementation
//Animal.cpp
#include "Animal.hpp"
#include <string>
Animal::Animal(){
roar = "...";
}
void Animal::shout(){
roar = "ROAR";
cout << roar;
}
This code generates my compiling issue. How would this issue be resolved?
Thanks for your time
EDIT
Undefined symbols for architecture x86_64:
"Animal::shout()", referenced from:
_main in test-5f7f84.o
"Animal::Animal()", referenced from:
_main in test-5f7f84.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
maybe you might want to see an alternative set of your 3 files, where things are a little more "sorted", you know, where things are put at places where they "really" belong to.
So here's the "new" header file ..
//Animal.hpp
#ifndef H_Animal
#define H_Animal
#include <string> // suffices
// Interface.
class Animal {
std::string roar; // private
public:
Animal();
void shout();
};
#endif
then the source file ..
//Animal.cpp
#include "Animal.hpp"
#include <iostream> // suffices
// Constructor.
Animal::Animal()
:
roar("...") // data member initializer
{}
// Member function.
void Animal::shout() {
roar = "ROAR";
std::cout << roar;
}
and the main program ..
//Main.cpp
#include "Animal.hpp"
int main(){
Animal thePet;
thePet.shout(); // outputs: `ROAR'
}
plus a little GNU makefile ..
all: default run
default: Animal.cpp Main.cpp
g++ -o Main.exe Animal.cpp Main.cpp
run:
./Main.exe
clean:
$(RM) *.o *.exe
Kick-off things typing just "make" in your cmd-line. Did you like it? --
Regards, M.
I can only find one error in your code and your compiler should have told you that one.
In Animal.cpp, you are using std::cout but you're not #includeing <iostream>. You #include it in Main.cpp but it is not needed there.
If you (really) want to refer to std::cout as cout in Animal.cpp, you also have to add a using namespace std directive in that file.
The using directive in the header file (Animal.hpp) is evil. Get rid of it and type std::string instead. Putting using directives into headers litters the namespaces of all files that use it.
I also don't understand your intentions with the roar member. What is the point of assigning "..." to it in the constructor and re-assigning "ROAR" to it every time shout is called? Couldn't you do without that variable and simply have
void
Animal::shout()
{
std::cout << "ROAR\n";
}
? I have added a newline because you'd probably want one.
The main issue I was having with this coding project was solved by #JamesMoore.
"#Nicholas Hayden Okay if you have three files, test.cpp(which has main), animal.cpp, and animal.hpp. The command should be g++ animal.cpp test.cpp. You need to compile all source files."
I am currently not using an IDE. So, when I was calling the compiler to compile my main.cpp - It was an issue of compiling the implementation file.
g++ test.cpp
needed to become
g++ test.cpp animal.cpp
This would call the compiler to compile everything the program needed.

Can't link other projects in my solution with premake

I'm trying to get started with premake but I can't get my test project to link properly with it. If I link it manual it works fine though.
I'm using premake 4.3 (also tested it with premake 4.4) on OS X 10.9 with clang 3.4.
After I create a makefile via "premake4 gmake" and try to compile it I get an error like this:
Linking subproject
ld: internal error: atom not found in symbolIndex(__ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [libsubproject.dylib] Error 1
make: *** [subproject] Error 2
My pretty simple project setup:
project/
src/
test.cpp
subproject/
include/
Library.hpp
source/
Library.cpp
premake4.lua
premake4.lua
solution "testa"
configurations {"debug"}
language "C++"
includedirs {"subproject/include"}
project "subproject"
kind "SharedLib"
files {"subproject/source/*.cpp"}
project "main"
kind "ConsoleApp"
files {"src/*.cpp"}
links {"subproject"}
src/test.cpp
#include <iostream>
#include <Library.hpp>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
Library lib(13, 3);
lib.do_stuff(7);
return 0;
}
subproject/include/Library.hpp
#ifndef __LIBRARY_HPP__
#define __LIBRARY_HPP__
#include <iostream>
using namespace std;
class Library {
public:
Library(int, int);
void do_stuff(int) const;
private:
int x;
int y;
};
#endif
subproject/source/Library.cpp
#include <Library.hpp>
Library::Library(int x, int y) {
this->x = x;
this->y = y;
}
void Library::do_stuff(int z) const {
cout << "X: " << x << "Y: " << y << "Z: " << z << endl;
}
Thank you for your time.
This is a known premake bug. It was reported and fixed, but a fixed version of the program has not been released yet. See the discussion here.
This bug is caused by -Wl,-x linker flags that premake will add by default to the project.make makefile. As of now, there are two possible solutions, download the updated premake source with the fix, compile it and install the new version, or, manually change the value of LDFLAGS in the generated project.make after each run of premake.
I have also tried the suggestion they give in the link above of setting premake.tools.gcc.ldflags.flags._Symbols to nil, but it had no effect on my system.

Error when trying to separating class into .h, .cpp

This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp

Undefined Reference using KDevelop 4.4.1

I'm a beginner with C++. I wrote the following:
// GradeBook.h
#include <iostream>
#include <string>
using namespace std;
class GradeBook {
public:
GradeBook(string); // constructor that initializes courseName
void setCourseName(string); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage(); // function that displays a welcome message
private:
string courseName; // course name for this GradeBook
};
// GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook(string name)
{
setCourseName(name);
}
void GradeBook::setCourseName(string name)
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}
// main.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
}
I am using KDevelop 4.4.1, then I proceed to execute my main.cpp and I got:
/home/brallan/projects/Hola/build> make
Linking CXX executable hola
CMakeFiles/hola.dir/main.o: In function main':
/home/brallan/projects/Hola/main.cpp:8: undefined reference to GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:9: undefined reference to GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:12: undefined reference to GradeBook::getCourseName()'
/home/brallan/projects/Hola/main.cpp:11: undefined reference to GradeBook::getCourseName()'
collect2: error: ld returned 1 exit status
make[2]: [hola] Error 1
make[1]: [CMakeFiles/hola.dir/all] Error 2
make: [all] Error 2
Failed
If I run the same code from Eclipse Juno CDT, it return me:
gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: CS102 Data Structures in C++
Can anyone help me to run it from KDevelop?
UPDATE: Based on the comments, KDevelop isn't compiling other files in the project :s
I guess this is the problem to be solved.
First, add the line #error (or any other syntax error) to the end of GradeBook.cpp. Ensure you get a compilation error for that line when you try to build it. If not, check spelling and capitalization of the file reference from the project or makefile.
If you do get a syntax error, or if you don't but you can't figure out why the file isn't being referenced, try this next: Remove the #error from GradeBook.cpp, and add #include "GradeBook.cpp" to the end of main.cpp. This serves two purposes: It get you going (should now be able to build and run) and it helps narrow the problem (if it works, you know the problem is with referencing GradeGook.cpp, rather than with its contents).
It seems you are not compiling GradeBook.cpp
In the project folder, there is a file called CMakeList.txt and on it are the files that are part of the project. I tried to add the file GradeBook.cpp to add_executable line, but still did not work. However, when I replaced the file names in lower case, and turn modify the line that I described, everything worked properly. I'm not sure what is the mistake if the file name has no upper or similarly if I add it to this list exactly as it is called.
Then, I renamed files gradebook.h and gradebook.cpp and added gradebook.cpp to add_executable line.