MinGW G++ not compiling - c++

I'm trying to compile a simple "hello world" C++ program but when I use the g++ command I get the error "undefined reference to `WinMain#16" this happens when I compile in vscode or in terminal, whats happening?
code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello World"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}

Your error is not a compiler, but a linker error.
That means it still compiles fine but fails to link.
A header file gets included (if it can be found in the include path the compiler uses to find headers), so this works fine.
But for linking two (object-)files together it has to know which files should be linked. And how should it decide which files to use?
That's the cause to create projects in it (and other IDE's) or to create makefiles.
If you turn on full command line-logging in "Settings -> Compiler and debugger... -> Global compiler settings -> [the_compiler_you_use] -> Other settings (rightmost tab) -> Compiler loggin" you can see which commands are sent to the compiler/linker and where the difference is between a single file and a project with multiple files.
By the way: your book/tutorial should explain the steps needed to compile and link files, what happens if headers are included and how to use prebuild libraries/dlls.

Related

How to compile/use header units in MSVC from command-line?

For example I have following toy files:
mod.hpp
#include <iostream>
use.cpp
import "mod.hpp";
int main() {
std::cout << "Hello, World!" << std::endl;
}
But if you compile it like cl use.cpp /std:c++latest then I get error
error C7612: could not find header unit for 'mod.hpp'
How do I create/use header units in MSVC?
NOTE: I'm making cross-platform/cross-compiler projects right now. It means that I want same sources to be able to compile in MSVC/CLang/GCC on both Windows and Linux. For me there is no point to make MSVC-specific extensions .ixx/.cppm, thats why I used .hpp/.cpp in my case. More then that I'm not making .vcxproj/.sln files at all, I'm only considered about low-level command line invocation for compiling in MSVC.
This question was made by me just to share my answer with ready-made solution.
To create precompiled header unit issue next command:
cl /EHsc /std:c++latest /exportHeader mod.hpp
this command creates mod.hpp.ifc file, which is a precompiled header unit module. Here is documentation about /exportHeader flag.
Then to use header unit issue command:
cl /EHsc /std:c++latest use.cpp /headerUnit mod.hpp=mod.hpp.ifc
documentation about /headerUnit is here. /headerUnit accepts param header-filename=ifc-filename. After command above final program compiles and outputs:
Hello, World!
This way you can precompile any header, including standard ones like import <iostream>;.
For commands above I used following files:
mod.hpp
#include <iostream>
use.cpp
import "mod.hpp";
int main() {
std::cout << "Hello, World!" << std::endl;
}

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

C++ run project as standalone files, rather than a Code::Blocks project

Good afternoon!
I'm currently working on a final project to close my first semester of C++ (and also programming in general) and I was wondering how I can properly link the implementation, header, and main files. The purpose is to be able to run them as just those 3 files, instead of being dependent on running them through a .project file.
This is an example of the project I'm doing:
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
Cat.h
#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif // CAT_H
CatMain.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
speak();
jump();
return 0;
}
The issue here is that when I open these files individually (without opening the .project file), I get the "undefined reference to WinMain#16" on Cat.cpp and "undefined reference" errors to the speak() and jump() functions on CatMain.cpp. The reason this might be an issue is because chances are that my professor won't have Code::Blocks to run it the same way as I do, so he will have to be able to run these standalone files rather than run the .project file.
Is there something I can do to link them together so that they're not dependent on the .project file to run properly?
Any help would be greatly appreciated =)
To run this or any C++ program without an IDE, you only need a C++ compiler available on the command line. (You may have to set an environment variable to the path to the compiler to use it on the command line)
To compile and run the code one could run the following commands on the command-line. (assuming a compiler such as GCC or MinGW, which uses the command g++ to compile C++)
g++ -c Cat.cpp (produces an .o object file for the implementation of Cat.cpp and Cat.h)
g++ CatMain.cpp Cat.o (compiles the main function as an executable and links the Cat.o to it)
a (in windows cmd)
./a.out (on *nix systems)

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

How do I get PCRE to work with C++?

This is a newbie question but I hope I can express my question as clearly as possible.
I'm trying to do pattern matching in C++.
I've downloaded the Win32 version of PCRE from here and I've placed the downloaded pcre3.dll and pcreposix3.dll files into the folder of Dev-CPP's lib folder (I'm using Bloodshed Dev-C++ 4.9.9 IDE).
I've also downloaded a pcrecpp.h header file and have it in the same directory I'm writing the following code (not writing actually. I'm coping example code from a PDF tutorial named PCRE- Perl Compatible Regular Express).
But I can't get it to work. The code is as follows:
#include <iostream>
#include <string>
#include <pcrecpp.h>
using namespace std;
int main()
{
int i;
string s;
pcrecpp::RE re("(\\w+):(\\d+)");
if (re.error().length() > 0) {
cout << "PCRE compilation failed with error: " << re.error() << "\n";
}
if (re.PartialMatch("root:1234", &s, &i))
cout << s << " : " << i << "\n";
}
When I compile the code, Dev-C++ gives me a lot of errors including: "`pcrecpp' has not been declared" and "RE" undeclared.
How should I deal with the downloaded files and fix my problem? Or is there something obvious that I'm missing?
If you specify the file for #include with angle brackets (<>), then the compiler will only look for that header in the locations for external libraries, in so far as the compiler is aware of them.
If you instead use quotation marks (""), then the compiler will also look in the locations for the current project, which typically includes the current directory.
The quick fix for your current problem is to use
#include "pcrecpp.h"
The alternative is to tell the compiler where it can find the headers of the PCRE library.
You will have to tell the compiler where it can find the headers of the PCRE library.
How to do this differs from build system to build system, but if you are using an IDE, then there should be an option somewhere to specify the 'Include directories'. This is where you add the directory of the PCRE headers (with full path).
As a side-note: When the compiler gives you a large number of errors and warnings, always start with fixing the first one. I would guess that in this case it was something like "unable to find header: pcrecpp.h".
It is often the case that, if the compiler tries to continue after encountering a problem, more problems are found that are follow-on problems of the first one. When the first problem is fixed, these also magically disappear.
g++ -lpcrecpp ......
you need to add '-lpcrecpp' to g++ command
cout << “PCRE compilation failed with error: “ << re.error() << “\n”;
I just copied your code and tried to compile it. I got the same error as you reported.
The problem is that string you put to cout is not properly started/ended. You should use real " instead of marks which looks like double quotes (") but it is not. If you fix it, your code should compile w/o any error.
You have included
#include <pcrecpp.h>
1st point to check But is file in the inlcude path of your code. Did you download the installable ? Check where it has been installed on your machine.
2nd point is to check do you have the library paths defined, so that they can be resolved during compiling and linking.