I am trying to do basic port programming, and it was suggested to me that I take a look at LibSerial.
I built and installed the package, but I am having issues accessing any SerialStream member functions
e.g. the following code (ls_ex.cpp) fails:
#include <SerialStream.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cerrno>
using namespace std;
using namespace LibSerial;
int main(int count, char* parms[])
{
if (count != 2)
exit(1);
//open port
string fname = parms[1];
SerialStream port(fname);
cout << port.isOpen() << endl;
port.Close();
return 0;
}
I am compiling it as so:
g++ -o ls_ex ls_ex.cpp /usr/local/lib/libserial.a /usr/local/lib/libserial.so
When I compile, I get the following error:
ls_ex.cpp: In function ‘int main(int, char**)’:
ls_ex.cpp:45:15: error: ‘class LibSerial::SerialStream’ has no member named ‘isOpen’
I assume I am compiling it wrong because its easy enough to look at the code and see that isOpen() is indeed public. Also, why am I even able to instantiate SerialStream just fine but the compiler blows up when I try to call any member function?
It is like this
g++ -o ls_ex ls_ex.cpp -lserial -L/usr/local/lib/
If you want the .a to be used instead of .so
g++ -o ls_ex ls_ex.cpp -static -lserial -L/usr/local/lib/
Be sure to specify your includes to your SerialStream.h as well
g++ -o ls_ex ls_ex.cpp -static -lserial -L/usr/local/lib/ -I/path/to/SerialStream
Related
I already searched and found a solution for this problem but i find this a little bit strange. Anyway my problem is this:
Personal.h
class Personal
{
public:
Personal();
int money;
~Personal();
}
Personal.cpp
#include "Personal.h"
Personal::Personal()
{
money = 1800;
}
Personal::~Personal(){};
Now i want to compile in main
main.cpp
#include "Personal.h"
#include <iostream>
#include <vector>
int main()
{
std::vector<Personal> test(100);
}
When I write: g++ -Wall main.cpp -o main it gives me :
undefine reference to Personal::Personal()
undefine reference to Personal::~Personal()
The solution:
g++ -Wall Personal.cpp main.cpp -o main
Why do i need compile the Personal.cpp too?
Or the other main version is to include instead of "Personal.h", "Personal.cpp"
main.cpp
#include "Personal.cpp"
#include <iostream>
#include <vector>
Then the normal g++ -Wall main.cpp -o main works
Can someone help me?
Why do i need compile the Personal.cpp too?
Because you use functions that are defined in that file. In particular, you use the functions Personal::Personal and Personal::~Personal.
Can someone help me?
Make sure that all functions (that are odr-used) are defined in exactly one (or in all files, in case of inline functions) of the source files that you compile and link together.
I try to compile & link the following program with mingw. When I use the default version it works well. But when I use c++11 version it doesn't compile and gives me the following error newfile.cpp:22:18: error: 'isblank' was not declared in this scope.
for testing the following program it's enough to call the _isblank function in main.
For compiling the Netbeans 8.0.2 uses g++ -c -g -Wall -std=c++11 -MMD -MP -MF "build/Debug/MinGW_1-Windows/newfile.o.d" -o build/Debug/MinGW_1-Windows/newfile.o newfile.cpp.
The mingw version is 4.8.1 and everything is configured well(Default).
I tried by adding/removing namespace std. The problem seems to be in cctype header! But I wonder how to solve it. The project will have to be compiled with g++ on linux! Will these problems remain?
#include <cstdlib>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <map>
#include <functional>
#include "newfile.h"
using namespace std;
conf_info_t CONF_INFO;
#define CONF_FILE_ADDRESS "confs.txt"
//
//typedef std::map<std::string, std::function<void (const std::string&)>> confMap_t;
//confMap_t confMap;
int _isblank(int c){
return isblank(c);
//return c == ' ' || c == '\t';
}
In my version of GNU library the declaration of isblank in <ctype.h> header is protected by some conditional compilation directives, which nevertheless should make this function available in C++.
However, in <cctype> header this function is is separated from all other declarations and given a special treatment for some reason. It is only made available in namespace std if macro _GLIBCXX_USE_C99_CTYPE_TR1 is defined. This is what it looks like inside <cctype>
#if __cplusplus >= 201103L
#ifdef _GLIBCXX_USE_C99_CTYPE_TR1
#undef isblank
namespace std
{
using ::isblank;
} // namespace std
#endif // _GLIBCXX_USE_C99_CTYPE_TR1
#endif // C++11
I don't know what the purpose that _GLIBCXX_USE_C99_CTYPE_TR1 macro is supposed to serve. In my GCC installation this macro is defined, which makes isblank available in my case.
You might want to check what your <cctype> looks like and see if something like that is happening on your side as well.
It worked when I undefined STRICT_ANSI in the translation unit adding -U__STRICT_ANSI__ to the compiler options. But I wonder which part of my program violates C++ standards.
It should have been compiled in this way:
g++ -U__STRICT_ANSI__ -c -g -Wall -std=c++11 -MMD -MP -MF "build/Debug/MinGW_1-Windows/main.o.d" -o build/Debug/MinGW_1-Windows/main.o main.cpp
I'm trying to create a c++ library for the purpose of controlling a signal generator. I have a working script that compiles into an executable that runs the way I want, and now I want to create a static library with a 'signal' class such that the code can be integrated into a larger library being created by the research collaboration I'm part of for all the hardware we have. However, I'm having trouble compiling a test program (test.cc) for the signal.cpp source code and signal.h that I've written.
Here's signal.cpp:
#include "signal.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
signal::signal(){
myfile.open("/dev/usbtmc1");
}
signal::~signal(){
myfile.close();
}
int signal::on(){
myfile<<"*RST\n";
myfile<<":DISPLAY OFF\n";
myfile<<"FUNC1:PULS:HOLD WIDT\n";
myfile<<"FUNC1:PULS:TRAN:LEAD 2.5NS\n";
myfile<<"FUNC1:PULS:TRAN:TRA 2.5NS\n";
myfile<<":FUNC1:PULS:WIDT 20NS\n";
myfile<<":FUNC1 PULS\n";
myfile<<":VOLT1 5.0V\n";
myfile<<":VOLT1:OFFS 1.797V\n";
myfile<<":FREQ1 100HZ\n";
myfile<<":OUTP1:IMP:EXT MAX\n";
myfile<<":ARM:SOUR1 IMM|INT\n";
myfile<<":OUTP1 ON\n";
myfile<<":OUTP1:COMP ON\n";
return 0;
}
int signal::off(){;
myfile<<"*RST\n";
return 0;
}
int signal::write(char comstring[80]){
strcat(comstring,"\n");
myfile<<comstring;
return 0;
}
Here's signal.h:
#ifndef SIGNAL_H
#define SIGNAL_H
#include <iostream>
#include <fstream>
#include <string.h>
//using namespace std;
class signal{
private:
std::ofstream myfile;
public:
signal();
~signal();
int on();
int off();
int write(char comstring[80]);
};
#endif
And the little test program I've written to try out calling the signal class:
#include "signal.h"
using namespace std;
int main(){
signal ser;
ser.on();
}
I can get the signal.cpp and the signal.h files to compile into a signal.so dynamic object, but when I try to call 'g++ test.cc -o test -l signal.h' at the terminal, I get the error:
signal.h:4:20: error: iostream: No such file or directory
signal.h:5:19: error: fstream: No such file or directory
signal.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘signal’
I'm confused by this, as I thought iostream and fstream were part of the c++ standard library and therefore wouldn't need to be linked when compiling using g++. Could anyone please illuminate me as to what I should fix or try to sort this? Many thanks. Sam.
Hoping your all files are in same folder.
g++ -I . test.cpp signal.cpp -o test
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have main.cpp:
#include "censorship_dec.h"
using namespace std;
int main () {
censorship();
return 0;
}
this is my censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void censorship();
this is my censorship_mng.cpp:
#include "censorship_dec.h"
using namespace std;
void censorship()
{
cout << "bla bla bla" << endl;
}
I tried to run these files in SSH (Linux), so I wrote: make main, but I got:
g++ main.cpp -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
please help!
You have to specify the file where censorship is defined.
g++ main.cpp censorship_mng.cpp -o main
You must add censorship_mng.cpp in your compilation command:
g++ main.cpp censorship_mng.cpp -o main
Another solution (if you really don't want change your compile command) is making void censorship(); to a inline function and move it from .cpp to .h.
censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}
And remove void censorship() from censorship_mng.cpp file.
once your project starts using several source-files to be compiled into a single binary, manual compilations become tedious.
this is usually the time when you start using a build-system, such as a Makefile
a very simple Makefile that uses default build-rules could look like
default: main
# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm
# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o
# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)
I am working in c++ /ubuntu.
I have:
libr.hpp
#ifndef LIBR
#define LIBR
#include <string>
using namespace std;
class name
{
public:
name();
~name();
std::string my_name;
std::string method (std::string s);
};
#endif
and
libr.cpp
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
name::name()
{
}
std::string name::method(std::string s)
{
return ("YOUR NAME IS: "+s);
}
From these two I've created a libr.a.
In test.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
int main()
{
name *n = new name();
n->my_name="jack";
cout<<n->method(n->my_name)<<endl;
return 0;
}
I compile with g++ and libr.a. I have an error: "name::name() undefined reference", why?
I would like to mention that I've added in qt creator at qmake the .a. When I compile, I have the error. How can I solve it?
This is a linker error, not a compiler error. It means that you have called but you have not defined the constructor. Your allocation name *n = new name(); calls the constructor.
Since you defined the constructor in your libr.cpp, what this means is that this compilation unit is not making its way into your executable. You mentioned that you are compiling with libr.a. When you compile your libr.cpp the result is a .o file, not a .a file.
You are not linking libr.o into your executable.
What are the steps you're using to compile your "project"?
I performed the following steps and managed to build it with warnings/errors.
g++ -Wall -c libr.cpp
ar -cvq libr.a libr.o
g++ -Wall -o libr main.cpp libr.a
One last thing, if I change the order off the last command, like
g++ -Wall -o libr libr.a main.cpp
I get the following error:
Undefined first referenced
symbol in file
name::name() /tmp/cc4Ro1ZM.o
name::method(std::basic_string<char, std::char_traits<char>, std::allocator<char
> >)/tmp/cc4Ro1ZM.o
ld: fatal: Symbol referencing errors. No output written to libr
collect2: ld returned 1 exit status
in fact , you needn't define the destructor yourself because the default destructor will be used when the class calling is over.
and in the VS2008,it's all right!