i am very new in build systems and i am trying to build c++ file with mingw compiler and makefile on windows.
i installed mingw and added it to PATH. My code as follows
//main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World !!!! "
return 0;
}
//Makefile
program:
g++ main.cpp -o program
i change my working directory as C:\MyDir which is included my .cpp and Makefile
cd C:\\MyDir
C:\\MyDir> make
i get the error
make *** no targets specified and no makefile found. stop ...
Do you have any idea about this problem, thanks..
Related
here is the file structure for my project file in c-language :
first is a fullCourse folder
fullCourse folder contains three folders :
1. include
2. src
3. lib
4. test
in include folder :
it contains a header file : temp.h
code for temp.h is as follow :
#ifndef __$__temp_h
#define __$__temp_h 234
int yash();
#endif
in src folder :
It contains a source file : temp.c
code for temp.cc is as follow :
#ifndef __$__temp_c
#define __$__temp_c 234
int yash()
{
return 22;
}
#endif
then staying in same folder, created .o file as follow :
gcc -I ../include -c temp.c
next step was moving this temp.o file to lib folder as follow :
mv temp.o ../lib
than staying in lib folder created a archived (or library) file as follow :
ar rcs libtmds.a temp.o
than in test folder wrote a source code to test library(tempTest.c)
code for tempTest.c is as follow :
#include<stdio.h>
#include<temp.h>
int main()
{
int w;
w=yash();
printf("%d\n",w);
return 0;
}
than staying in test folder tried to compile it as follow :
gcc -static -I ../include tempTest.c -L ../lib -ltmds -o tempTest.exe
but code didn't compiled, showing following error :
ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
please help me resolve this problem.
-static is in error here. To link to your static lib just plain link to it without any -static flag, which is for kernel compilation.
I'm trying to build the file which is presented at the UNC path. Here I'm providing simple example to get more clarity.
main.cpp
#include <iostream>
#include "additionApi.hpp"
int main(){
int a=10, b=20;
std::cout<<"Addition:"<<addNumbers(a,b);
return 0;
}
tmpOperation\additionApi.hpp
int addNumbers(int a, int b);
tmpOperation\additionApi.cpp
#include <iostream>
int addNumbers(int a, int b){
return a+b;
}
tmpOperation\Makefile
tmpOperationDir = \\unc\path\location\tmpOperation
%.obj:%.cpp
cl /TP /Od /Oy- -Zi /EHsc -I"." -I"$(tmpOperationDir)" /c $< /Fo$#
../math_exe:additionApi.obj ../main.obj
link /OUT:../math.exe additionApi.obj ../main.obj
all:../math_exe
In the above file structure, I'm using make command from the tmpOperaion directory. main.cpp is presented at outside of the tmpOperaion directory. So I'm accessing main.cpp using the relative path as ../main.cpp in Makefile. But it is unable to build the main file and giving the following error.
gmake: *** No rule to make target `../main.obj', needed by `../math_exe'. Stop.
So finally, below are my straightforward questions:
Can we build a file(main.cpp in the given example) relatively from the UNC path ?
Does UNC paths are supports file inclusion in make file like -I"$(UNC_PATH)" ?
We may have to do this in several steps.
First, change this:
%.obj:%.cpp
to this:
../%.obj:%.cpp
And try make ../main.o and tell me the result.
The question here gives an example of how to use xgboost - a machine learning library written in C++. I want to run the example, and therefore install the library for C++. I added the /lib files to /usr/local/lib and /src files to /usr/local/src
I am able to compile this much part of the example :
#include <iostream>
#include <xgboost/c_api.h>
using namespace std;
int main(){
int cols=3,rows=5;
float train[rows][cols];
for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
train[i][j] = (i+1) * (j+1);
float train_labels[rows];
for (int i=0;i<rows;i++)
train_labels[i] = 1+i*i*i;
DMatrixHandle h_train[1];
XGDMatrixCreateFromMat((float *) train, rows, cols, -1, &h_train[0]);
}
However, I am getting a linking error:
/tmp/ccuBacNh.o: In function `main':
TerrainPredict.cpp:(.text+0x278): undefined reference to `XGDMatrixCreateFromMat'
collect2: error: ld returned 1 exit status
How can I resolve this. I concluded that cpp files are missing, but I don't know where to put them. I want help regarding how to proceed with installation.
I also tried using a makefile:
LDLIBSOPTIONS=xgboost/lib/libxgboost.a xgboost/rabit/lib/librabit.a xgboost/dmlc-core/libdmlc.a -lpthread
CFLAGS=-I xgboost/include -I xgboost/rabit/include -I dmlc-core/include
all:
g++ main.cpp $(CFLAGS) $(LDLIBSOPTIONS)
I put Makefile, main.cpp and xgboost directory in the same folder.
In that case I am getting a longer linking error. Error is too long to include fully. Here it is.
I am trying to compile a source given a .so file libfoo.so. The only thing in this library is a function that just returns a number (yeah, I know, advanced stuff). The header file equivalent (I was provided with both, but am only supposed to use the .so) is named foo.h and the function is named int foo().
My source file is main.c:
#include <stdio.h>
#include "foo.h"
int main()
{
int x = foo();
printf("%d", x);
return 0;
}
Now, when trying to compile I have the following commands:
gcc -Wall -fPIC -c main.c -o main.o
gcc -Wall -fPIC main.o -o main -lfoo -L.
The first command fails to create the object file, outputting the following error:
fatal error: foo.h: No such file or directory
I am using Ubuntu 16.04.
I have also tried exporting the current location to LD_LIBRARY_PATH as I've seen suggested on a few other answers.
export LD_LBIRARY_PATH=$LD_LIBRARY_PATH:machine/Desktop/lib_test
You need to have the interface definition from the .h file and that file must be in the current directory or a directory on the include search path.
Note that on some systems filenames and paths are case dependent.
I am trying to learn the make command but I am having a little trouble with the way headers are being used
I got prg1.c
#include <stdio.h>
main()
{
printf("Hello World\n");
print();
}
and the prg2.c
#include <stdio.h>
print()
{
printf("Hello World from prg2\n");
}
and here is the make file
objects = prg1.o prg2.o
exe : $(objects)
cc -o exe $(objects)
prg1.o : prg1.c
cc -c prg1.c
prg2.o : prg2.c
cc -c prg2.c
This works perfectly. But if I don't include stdio.h in both file and then I have to compile it using the make, how am I supposed to write the makefile?
If you don't include <stdio.h>, then you can do one of two things:
supply a correct declaration for printf yourself:
int printf(const char *fmt, ...);
There is almost never any reason to do such a thing.
If your compiler is GCC, use the -include compiler option to force the inclusion of "stdio.h":
prg1.o: prg1.c
gcc -c -include stdio.h prg1.c
This is completely hokey; don't do it.
Note that the make utility has nothing to do with ensuring that the correct header material is included in C translation units. make is a utility which runs shell commands in response to some files not existing or having modification stamps older than other files.