Simple C++ program using pqxx (postgres) - c++

I'm trying a very basic C++ program using Code::Blocks. I'm on Ubuntu 12.04 and installed pqxx from the software manager. Here's the code.
#include <pqxx/pqxx>
#include <iostream>
using namespace std;
int main()
{
pqxx::connection MyConn ("dbname=dbESM user=postgres");
cout << "Hello world!" << endl;
return 0;
}
But I get the following error on hitting F9 to compile and run:
/usr/include/pqxx/connection.hxx|87|undefined reference to
`pqxx::connectionpolicy::connectionpolicy(std::basic_string, std::allocator > const&)'
The above message is from the file connection.hxx and the line highlighted is this:
explicit connect_direct(const PGSTD::string &opts) : connectionpolicy(opts) {}
The connection.hxx file is not mine - I think it's part of pqxx.
I'm pretty new to this platform so I'm avoiding the terminal to compile code. Any help would be greatly appreciated.

You need to add the reference to the libpqxx library to the project.
Inside Code::blocks, when the project is open, locate Project in the menus, then follow Build options, then open the tab called Linker settings, then hit Add, then enter pqxx.
If you were using the libpq C library instead, the procedure would be identical except the name would be pq.

You need to link against the according library, just #including the header files isn't enough. If available, you could use pkg-config to determine the according libraries. Further, what IDE are you using? Without that, the "on hitting F9" reference is useless. Also, compiling this on the commandline might even be easier, since it is clearer what exactly is happening.

Related

Eclipse C++ Setup

I want to switch from Dev-C++ to Eclipse (Version 12/18, 4.10.0) , but I am not able to Compile in Eclipse. I installed MinGW, edited the Pathvariable, restared the PC, installed the C++ Eclipse IDE, created a new Project there I choosed MinGW as my Compiler, and wrote a Hello World.
The Code is:
#include<iosteam>
using namespace std;
int main(){
cout << "Hello World!";
return 0;
}
The Error Message I get is the following:
error: CreateProcess: No such file or directory
What do I need to do to be able to build/run Proramms?
Thanks in advance
You have everything you need to be able to build/run programs. Maybe something is flawed in the configuration. You should separate the tasks to find out the problem.
First try to compile your minimal example by executing g++ from a console like that:
g++ main.cpp. If you get the same error message then you have to fix your Mingw-w64 setup.
If this works, then something is wrong with eclipse. You might want to check different project types. When using Makefile projects, you perhaps have more possibilities to do checks on the console.

CImg Compilation Error: t_normal not in global namespace

I'm currently working on a class assignment that requires the use of the CImg library. To be clear, the assignment is not linking the library into the program; The class is using it access the pixel data for later use in the heart of the assignment.
I'm working in Xcode (OS X 10.10). CImg (2.2.2) is installed from homebrew, and I've managed to navigate the weird way Xcode deals with search paths (added the header to the section), and have successfully-ish included CImg.
my full code is as below.
#include <iostream>
#define cimg_display 0 //I don't need X11 at all
#include "CImg.h"
using namespace cimg_library;
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
However, I get 17 Compile-time errors from CImg.h, which are very unusual, and all of the form:
"No member named 't_normal' in the global namespace; did you mean simply 't_normal'?"
Thinking I might have received a bad download, I have attempted to redownload CImg, with no luck. I have also gotten to this same point with non-homebrew versions of CImg.
To verify the download, I also compiled the examples from the command line and they ran perfectly.
Is there a problem with CImg that I'm not aware of, a problem with Xcode that I'm not aware of, or is there something fundamental that I'm missing (definitely an option, my C-style programming is a little rusty) ?
halp pls.
Your code runs fine if you do this:
Create a new Xcode project, with:
type = "Command Line Tool"
language = "C++"
Then go to "Build Settings" and add the path to the directory containing CImg.h to your "User Header Search Paths"

String variables don't work in Eclipse CDT

When I use string variables in Eclipse CDT (MinGW compiler) and I run the program, it doesn't show me anything. Here's my code:
#include <iostream>
using namespace std;
int main() {
string hw = "Hello, world!";
cout << hw << endl;
return 0;
}
So that doesn't show anything, but when I just do cout << "Hello, world!" << endl; it does work.
I also tried including <string>, <string.h>, "string" and "string.h" but still no success. The same code does work in VC++ and Codeblocks though.
I can reproduce this problem on my machine. The output does not appear. But it is not an output problem, but actually two problems before you get to main:
The program is failing to launch. If you try double-clicking on it in Windows Explorer, you will get an error message like this:
The program can't start because libgcc_s_dw2-1.dll is missing from
your computer. Try reinstalling the program to fix this problem.
Screenshot of above:
When launched from within Eclipse, this error message is silently swallowed, so how are you supposed to know!
Solutions/Workarounds
You need to get the environment correctly set up to launch the MinGW program because its DLLs are not installed on Windows PATH or other standard places DLLs are searched for.
Add C:\MinGW\bin to your PATH
Launch Eclipse from within a MinGW shell (has basically same effect as 1)
Run the program in debug mode, this causes the program to be launched as a child of GCC and that sets up
Other options (not tried by me) may include statically linking the offending library, see The program can't start because libgcc_s_dw2-1.dll is missing
File a CDT bug about the error message being hidden.
Extra info
If your program compiles, as I am sure it does based on your comments, changing the includes is probably irrelevant.
By adding an infinite loop around the couts I could immediately identify something more than simply an output not being shown was going on. Try the same thing on your machine, and also try running the program from within MinGW shell and from outside it.

C++: Why does libtiff break the console-output?

So finally I’m not able to help myself out by researching anymore. Hopefully you can help me.
I recently decided to learn C++ in the context of my bachelor-thesis: My first aim is to read the pixel-values of a tiff-image with the libtiff-library. Yet, every call of a function of the library seems to break parts of my program.
Here’s the simple “HelloWorld”-Program, it works as it should:
#include "tiffio.h"
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
// TIFF* tif = TIFFOpen("path to .tif", "r");
return 0;
}
When I uncomment the second line in main(), the code still does compile without errors (except the warning that ‘tif’ isn’t used) and I can start the program as usual. Yet nothing gets printed into the console anymore. Not “Hello” nor any errors.
Any suggestions where the error could be? The code should be alright, I guess I messed something up during the setup of the library or so. Here’s what I did:
I managed to set up eclipse (Mars, 32bit) for C++ and MinGW (32bit) on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
I created a new C++-project with the mentioned code and did the following adjustments in the project-properties:
Project->Properties->C/C++ General->Paths and Symbols->Library
Paths-> Added “D:/… /tiff-4.0.4/libtiff/.libs”
Project->Properties->C/C++ Build->MinGW C++
Linker->Miscellaneous->Set Linkerflags to “-static-libgcc
-static-libstdc++”
Project->Properties->C/C++ Build->MinGW C++ Linker->Libraries-> Set
(-L) to “D:/… /tiff-4.0.4/libtiff/.libs” and (-l) to “libtiff”
I know the .tif is a valid file as I implemented parts of my program in C#, using the LibTiff.NET library by BitMiracle.
Thank you in advance!
Edit 1: The same error occures, even if TIFF* tif = TIFFOpen("path to .tif", "r"); is never called but written down in a dead part of the code. Debugging does not work either, the program seems to be terminated (exit value 0) before a single line is executed, without any error-reports.
I had the same issue and managed to get rid of it.
I set up eclipse (Mars) for C++ and MinGW on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
After the build, I got two directories containing files:
include
tiff.h
tiffconf.h
tiffio.h
tiffio.hxx
tiffvers.h
lib
libtiff.a
libtiff.dll.a
libtiff.la
libtiffxx.a
libtiffxx.dll.a
libtiffxx.la
I also linked all include files and only the libtiff.a to my project and that solved it, ie, the other lines are now executed.
I hope, I helped with this post.

undefined reference to function code blocks

main.cpp
#include <iostream>
#include <string>
using namespace std;
void echo(string);
int main()
{
echo("hello");
cout << "Hello world!" << endl;
return 0;
}
print.cpp
#include <iostream>
#include <string>
void echo(string code){
cout << code;
}
After compiling the code in code blocks 12.11, it gives me that error:
undefined reference to `echo(std::string)
I use windows 7 x64.
I have added the directory; Project>build options > search directories and added the current working directory.
All the files are in one console project in code blocks
I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.
Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:
When you are writing code over multiple files
Compartmentalize your code into separate blocks.
Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.
Anyways, try changing your code to this:
#include <iostream>
#include <string>
void echo(std::string code){
std::cout << code;
}
Then your results will look like this:
> g++ main.cpp print.cpp -o a.out
> ./a.out
helloHello world!
You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.
Change to e.g.
void echo(std::string code) { ... }
And you do try to link with the object file created from print.cpp ?
I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!
Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).
Some usefull information on Project management in CB
http://www.codeblocks.org/docs/main_codeblocks_en.html
When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help