Jump to content

Hey there guys,

 

so I'm doing some C++ programming, just starting out, and I have this stupidly simple program, and I can't get it to compile using GNU g++ compiler.

The code works when I run it in Visual Studio but not when I try to compile and run through Command Prompt.

 

here is a screenshot of the error i get.

 

and here is my source code as is

 

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    string one = "This is a string variable value";
    int two = 2;

    cout << "Learning to print items to console is one of the first things we learn" << endl;

    cout << "At this point I have decalred a string and integer variable, here they are:" << one << two << endl;

    string in;

    cout << "Now I'll get a user input" << endl;

    cin >> in;

    cout << "Here's the input you entered" << in << endl;

    return 0;
}

Screenshot (1).png

Link to comment
https://linustechtips.com/topic/785243-c-compiling-trouble/
Share on other sites

Link to post
Share on other sites

5 minutes ago, TAHIRMIA said:

If you can compile it in VS why do you want to compile in G++?

Cause VS does some leg work for you and I want to learn the raw basics before I have an IDE do work for me. Someone above mentioned I need to add something that VS does for me which is causing the error.

 

Also I wanna get into the habit of not being entirely reliant on IDEs.

Link to comment
https://linustechtips.com/topic/785243-c-compiling-trouble/#findComment-9896454
Share on other sites

Link to post
Share on other sites

5 minutes ago, BrownZeus said:

How do i do that? and if you wouldn't mind, could you explain why this is necessary?

According to https://stackoverflow.com/questions/1316747/how-is-the-c-standard-library-linked-to-my-application It looks like you need to link a DLL, specifically MSVRCT.dll for C programs and MSVCP60.dll for C++ programs.

 

There's also these (for MinGW and Cygwin):

http://www.mingw.org/wiki/specify_the_libraries_for_the_linker_to_use

https://cygwin.com/cygwin-ug-net/dll.html

 

(I'm fresh in this topic since I don't compile using GCC on Windows)

Link to comment
https://linustechtips.com/topic/785243-c-compiling-trouble/#findComment-9896470
Share on other sites

Link to post
Share on other sites

10 minutes ago, Nicholatian said:

Look in the GCC help page; the library should be called "libstdc++".

 

Compilers don't know what code you're linking to, that's the linker's job. But the linker needs to know where all of the standard library code exists so when your program is compiled, it can work. This is true no matter what toolchain you are using, however many toolchains see it as better to auto-link the standard library in by default... leaving people like you who are ignorant of the process confused when it's time to try out your code elsewhere.

 

The thing is though, G++ should be doing that just like MSVC does... and upon further inspection, your screencap shows that you aren't using G++. You're using GCC. Try running it with g++ instead and it ought to work.

Thanks for the info! Very much appreciated.

 

I tried your g++ suggestion and got the following error in the attached screenshot.

Screenshot (2).png

Link to comment
https://linustechtips.com/topic/785243-c-compiling-trouble/#findComment-9896498
Share on other sites

Link to post
Share on other sites

Since you're dabbling in GCC, I think it would be prudent as well to learn about make and make files. So perhaps this could help you too: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

Link to comment
https://linustechtips.com/topic/785243-c-compiling-trouble/#findComment-9896542
Share on other sites

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

Since you're dabbling in GCC, I think it would be prudent as well to learn about make and make files. So perhaps this could help you too: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

Thank you very much!

Link to comment
https://linustechtips.com/topic/785243-c-compiling-trouble/#findComment-9896549
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×