I am having trouble with make. I installed all of the features that come with cygwin. When I type in make command it says:
make:***No targets specified and no
makefile found. Stop.
This is really annoying as it's a very simple program.
My make file (Makefile):
# This is a sample file.
all: lab1
lab1: lab1.o
g++ -Wall lab1.o -o lab1
lab1.o: lab1.cpp
g++ -Wall -c lab1.cpp -o lab1.o
my program (lab1.cpp):
// This is a sample file written.
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to CS-240!" << endl;
return 0;
}
The issue (fixed in the comments) was that there was a Makefile.mak in the directory but not an actual Makefile without an extension. Changing the name worked for the submitter.
Related
while using g++ (version 4.9.2) from MinGW provided with Code::Blocks IDE, I am getting incredibly huge .exe files, going up to 1000KB for a "Hello, World!" program. I use exactly the same compiling options as like it would be compiled from "inside" of Code::Blocks (mingw32-g++.exe -Wall -g hello.cpp -o hello.exe, anyway, i just copy it from the Code::Blocks log window), but unlike that way, which produced about 70KB output for Debug target and 50KB for Release, this keeps on making incredibly large output.
Did anyone else meet a similar issue?
[EDIT] My project (created only for testing Makefiles purposes) consists on two files, main.cpp and fun.cpp (no headers). Here is main.cpp:
#include<iostream>
using namespace std;
void fun(void);
int main()
{
cout<<"Hello Make!"<<endl;
fun();
return 0;
}
and here is fun.cpp:
#include<iostream>
using namespace std;
void fun()
{
cout<<"Hello from The Module!"<<endl;
}
And finally, this is my Makefile:
CC = mingw32-g++
CFLAGS = -Wall -Os -lto
all: main.o fun.o
${CC} -o hello.exe $^
relink:
mingw32-g++ -o hello.exe hello.o fun.o
main.o: main.cpp
mingw32-g++ ${CFLAGS} -c main.cpp -o $#
fun.o: fun.cpp
mingw32-g++ ${CFLAGS} -c fun.cpp -o $#
clean:
del *.o
When compiling this "project" under Code::Blocks I get the following
log
When using Makefile, the output file is 1024KB large:
(print screen)
Debug builds will usually be larger than optimized (aka "release") ones. Try adding -O2 or -O3 to your build options (or -Os to specifically optimize for size).
Also, if you don't need the debug symbols in a release build then remove -g (or remove them afterwards with the strip command).
Also; compiling with Link-Time Optimization can sometimes yield a size reduction (in addition to a performance improvement) - for that use the -lto option (in addition to one of the -O options).
The above should give you a significant size reduction.
See the gcc manual for more details on the options.
This is my first go at making my own header file. I am trying to make a simple Hello World program in C++ on Ubuntu. made 3 files as follows :
//hello.h file
#ifndef HELLO_H
#define HELLO_H
//my own code
void hello();
#endif
//hello.cpp file
#include <iostream>
#include "hello.h"
using namespace std;
void hello()
{
cout << "This line is printed from header.";
}
//main.cpp file
#include <iostream>
#include "hello.h"
using namespace std;
int main()
{
cout << "in main" << endl << endl;
hello();
return 0;
}
I've tried
g++ -I /home/Desktop/hello/ -c hello.cpp -o hello.o
to compile header file and this command worked.
then, while doing
g++ -o main main.cpp
I am ending up with following error:
/tmp/ccb0DwHP.o: In function `main':
main.cpp:(.text+0x2e): undefined reference to `hello()'
collect2: error: ld returned 1 exit status
Please suggest whether changes need to be made in any file or in any command in the terminal?
thank you
You don't link to hello.o in the command below:
g++ -o main main.cpp
Try this:
g++ -o main main.cpp hello.o
Or for such simple program, just issue the command below:
g++ -o main main.cpp hello.cpp
For ease of use, create a makefile, then you just run make:
make
A simple makefile:
helloprogram: hello.h hello.cpp main.cpp
g++ -o helloprogram main.cpp hello.cpp
clean:
rm helloprogram
Put hello.h in Path2Hello;
g++ -o main -I Path2Hello main.cpp hello.cpp
ps: -I option to specify an alternate include directory (for header files).
To compile and run a C language program, you need a C compiler. To setup a C language compiler in your Computer/laptop, there are two ways:
Download a full fledged IDE like Turbo C or Microsoft Visual C++, which comes along with a C language compiler.
Or, you use any text editor to edit the program files and download the C compiler separately.
I wanted to use the unordered_map STL in c++, but as soon as I use the header, it gives me this error:
This file requires support for the compiler and library support for the ISO C++11 standard. This support is currently experimental and must be enabled with -std=c++11 or -std=gnu++11 compiler options.
I am attaching my code that I wanted to run, below. (Any inputs on the code are welcome too. thanks)
#include <iostream>
#include <unordered_map>
using namespace std;
class Node
{
public:
string a,b;
Node()
{
a="hello";
b="world";
}
};
int main ()
{
unordered_map<Node> mymap;
Node mynode;
mymap.insert(mynode);
std::cout << "myrecipe contains:" << std::endl;
for (auto& x: mymap)
std::cout << x.a << ": " << x.b << std::endl;
}
Edit: I got it to work by using the following commmand: g++ -std=c++11 [filename].cpp
Thanks for the help.
The main answer to your question: specify -std=c++11 in your compile command.
Precisely which C++11 features are available will depend on your version of GCC. Here are two links that might help:
https://gcc.gnu.org/projects/cxx0x.html
http://wiki.apache.org/stdcxx/C++0xCompilerSupport
First Option:
You can remove to error with -std=c++11 in compile time.
g++ -o binary yourFile.cpp -std=c++11
Second Option to integrate the development with c++11:
You can use a makefile with the CXXFLAGS set with -std=c++11 A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.
Here is the code :
CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog
SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)
all: $(OBJ)
$(CXX) -o $(BIN) $^
%.o: %.c
$(CXX) $# -c $<
clean:
rm -f *.o
rm $(BIN)
It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.
compile with:
g++ -o binary source.cpp -std=c++11
This is not actual code i am working on but sample code i had written to understand what i am doing wrong. So i have three files main.cpp, favourite.cpp and favourite.h. I am trying to compile main.cpp but get some weird error.
// main.cpp File
#include <iostream>
#include "favourite.h"
using namespace std;
int main()
{
favNum(12);
}
// favourite.cpp File
#include "favourite.h"
#include <iostream>
using namespace std;
void favNum(int num)
{
cout << "My Favourate number is " << num << endl;
}
// favourite.h File
#ifndef FAVOURITE_H
#define FAVOURITE_H
void favNum(int num);
#endif
This all files are in same folder and i am compiling it normally like g++ main.cpp I am not sure if i need to compile it diffrently as i am using custom header files.
If you say g++ main.cpp and this is your whole command line, the error is a linker error that it can't find favNum, right? In that case, try:
g++ main.cpp favourite.cpp
or split compilation and linking:
g++ -c main.cpp -o main.o
g++ -c favourite.cpp -o favourite.o
g++ main.o favourite.o
Where -c means: Compile only, no linking and -ofilename is required because you want to write the output to two different object files to link them with the last command.
You might also add additional flag, the most important ones are:
-Wall -Wextra -O3
Oh I guess I see the error although you should have included it in your question.
When compiling multiple source files you need to list them all on the GCC command line. Or you can use a Makefile.
So you could do this:
g++ favourite.cpp main.cpp
Or you could write a Makefile like this:
all: program
program: main.o favourite.o
And then just type:
make
I've written a very simple c++ function in main.cpp:
#include <iostream>
using namespace std;
int SomeCalculation(float x){
int decision = 0;
if (x > 1){
decision = 1;
}
return decision;
}
I'm now trying to compile this as a shared library using Boost.Python. For this I created decision.cpp:
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(decision)
{
using namespace boost::python;
def("main", main);
}
Unfortunately I get the following error:
In file included from /usr/include/boost/python/detail/prefix.hpp:13:0,
from /usr/include/boost/python/args.hpp:8,
from /usr/include/boost/python.hpp:11,
from decision.cpp:1:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: File or folder does not exist.
compilation terminated.
Since I had no clue of where this file could be I did a simple sudo find / -name pyconfig.h, which found several pyconfig.h files. So I simply copied what to me seemed the most general version of the file, to the folder in which I'm working:
cp /usr/include/python2.7/pyconfig.h /home/kram/c++/cmod/pyconfig.h
Running my compile command again (g++ -fPIC -g -ggdb -c decision.cpp -o decision.so) gives me the same error as before though.
Does anybody know how I can solve this pyconfig.h dependency?
[edit] Added pieces of code
Try command:
g++ -g -shared -fPIC -I/usr/include/python2.7 decision.cpp -lpython2.7 -lboost_python -o decision.so