我正在学习c并设法使我的头在makefile周围。我正在努力学习C语言。
在练习2中,多余的信用声明要在我的Makefile中创建一个命令,该命令使我可以通过仅键入“ make”来编译.c文件。
我的makefile是:
CLAGS=-WALL -g
all: ex1
clean:
rm ex1
Run Code Online (Sandbox Code Playgroud)
这有效-即键入make clean将删除ex1文件,然后键入make将ex1.c编译为可执行文件。
但是,我对为什么在clean命令中rm ex1必须在clean下面的行上缩进感到困惑,但是对于all命令,如果我在所有下面的行中缩进ex1,则会引发错误。为什么是这样?
我的另一个问题是-这个工作原理如何?难道所有的关键词都只是“键入make(不带命令)时执行此操作”吗?
任何帮助表示赞赏。
谢谢。
我刚刚发现我一直在做这个错误.我没有使用任何ide,只使用gcc.我也开始使用makefile来编译我的大项目.
大多数时候文件结构都是这样的
??? makefile
??? src
??? folder1
? ??? header1.cpp
? ??? header1.h
??? folder2
? ??? header2.cpp
? ??? header2.h
??? main.cpp
Run Code Online (Sandbox Code Playgroud)
在header2.cpp,当我包括header1.h我这样做
file header2.cpp
#include "../folder1/header1.h"
Run Code Online (Sandbox Code Playgroud)
这是我如何包含其他文件夹中的其他文件.我想我做错了.我观看的大多数教程都使用了Ide,它们并没有像这样包含它.
有些像这样包括它
#include "folder1/header1.h"
Run Code Online (Sandbox Code Playgroud)
或者其他人把它放在一个像这样的文件夹中 headers/
然后像这样包括它.
#include "header1.h"
Run Code Online (Sandbox Code Playgroud)
任何人都可以指导我.我该如何实现这一目标.我猜这一点我做得很糟糕.
我不想包含这样的文件
#include "../../../../sofarfolder1/header1.h"
Run Code Online (Sandbox Code Playgroud)
谢谢.它让我每次看到我的代码时都会呕吐.
考虑一个包含(仅)通过以下方式获得的3个文件的目录:
echo "foobar" > test1.txt
echo "\$foobar" > test2.txt
echo "\$\$foobar" > test3.txt
Run Code Online (Sandbox Code Playgroud)
(并且因此分别含有foobar,$foobar,$$foobar).
该grep指令:
grep -l -r --include "*.txt" "\\$\\$" .
Run Code Online (Sandbox Code Playgroud)
过滤包含双倍美元的文件(实际上是唯一文件):
$ grep -l -r --include "*.txt" "\\$\\$" .
./test3.txt
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在,该指令在makefile中失败,例如:
doubledollars:
echo "Here are files containing double dollars:";\
grep -l -r --include "*.txt" "\\$\\$" . ;\
printf "\n";\
Run Code Online (Sandbox Code Playgroud)
导致错误:
$ make doubledollars
echo "Here are files containing double dollars:";\
grep -l -r --include "*.txt" "\\\ . ;\
printf "\n";\ …Run Code Online (Sandbox Code Playgroud) 我有一个简单的C程序我必须做(不能使用main函数).
因此:
test.c的
#include <stdio.h>
int getUserID(int id);
int getUserID(int id) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我现在的函数现在什么都不做:但是,我试图通过makefile编译它,如下:
all:
gcc test.c
Run Code Online (Sandbox Code Playgroud)
哪个没用,因为我没有主要的,所以我在其中添加了-c命令.
all:
gcc -c test.c
Run Code Online (Sandbox Code Playgroud)
现在使用make编译,但是给了我一个目标文件(但是没有可用的可执行文件),我尝试运行它时得到的可执行文件: i.e ./test tells me permission denied
任何帮助将不胜感激.
我有四个文件 list.h list.c test_list.c Makefile
list.h
#ifndef List_H
#define List_H
#endif
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)
list.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)
test_list.c
#include "list.h"
#include <stdio.h>
int main(){
return 0;
}
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)
Makefile文件
CC=cc
CXX=CC
CCFLAGS= -g -std=c99 -Wall -Werror
all: list test_list
%.o : %.c
$(CC) -c $(CCFLAGS) $<
test_list: list.o test_list.o
$(CC) -o test_list list.o test_list.o
test: test_list
./test_list
clean:
rm -f core *.o test_list
Run Code Online (Sandbox Code Playgroud)
当我在shell中输入make时,出现错误:
/ usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line):重定位0具有无效的符号索引2/usr/lib/gcc/i686-linux- gnu/4.8 /../../../ i386-linux-gnu/crt1.o:在函数
_start':(.text+0x18): undefined reference tomain'collect2:error:ld返回1退出状态make:***[list]错误1 …
我正在尝试在运行特定目标时要求在Makefile中设置环境变量.我正在使用该问题的答案中的技术,您可以在其中设置另一个目标,以确保设置环境变量.
我看起来像这样:
require-%:
@ if [ "${${*}}" = "" ]; then \
$(error You must pass the $* environment variable); \
fi
Run Code Online (Sandbox Code Playgroud)
使用该目标设置,这是预期的:
$ make require-FOO
Makefile:3: *** You must pass the FOO environment variable. Stop.
Run Code Online (Sandbox Code Playgroud)
但是,在测试时,我永远不会得到它没有错误:
$ make require-FOO FOO=something
Makefile:3: *** You must pass the FOO environment variable. Stop.
$ make require-FOO FOO=true
Makefile:3: *** You must pass the FOO environment variable. Stop.
$ make require-FOO FOO='a string'
Makefile:3: *** You must pass the FOO environment …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的目录结构:
$ tree
.
|-- dir
| `-- subdir
| `-- data
`-- makefile
Run Code Online (Sandbox Code Playgroud)
哪里data是文件.我makefile看起来像这样:
all: dir/analysis
.SECONDEXPANSION:
%/analysis: %/subdir
%/analysis: $(addsuffix /data, $$^)
@echo TARGET: $@ DEPS: $^
touch $@
Run Code Online (Sandbox Code Playgroud)
当我跑步时make,我希望结果看起来像这样:
TARGET: dir/analysis DEPS: dir/subdir/data dir/subdir
touch dir/analysis
Run Code Online (Sandbox Code Playgroud)
相反,它只是报道
make: *** No rule to make target `dir/analysis', needed by `all'. Stop.
Run Code Online (Sandbox Code Playgroud)
如果我将第一个规则更改为dir/analysis: dir/subdir然后它按预期工作.因此我怀疑make忽略了第一条规则,并在第一条规则出现时直接跳到第二条规则%/analysis: %/subdir.当两个规则都dir/analysis作为目标而不仅仅是第一个规则时,它也可以按预期工作.MadScientists对这里不同问题的回答似乎适用于我的问题.我尝试添加类似的规则
dummy_target: dir/subdir
mkdir -p dir/subdir
Run Code Online (Sandbox Code Playgroud)
和
dir/subdir:
mkdir -p …Run Code Online (Sandbox Code Playgroud) 我需要在c ++项目中包含dlib.我已经在我的Makefile中正确链接了库.这是Makefile:
EXE = face_frontalization
OBJ_DIR = bin
CFLAGS = -g -w
# CXXFLAGS = -w -Wall -Wextra -g -std=c++0x
# LDFLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lSDL2_gfx -lm
dummy_build_folder := $(shell mkdir -p $(OBJ_DIR))
# c++ source files of the project
CXXFILES = $(shell find src -type f -name '*.cpp')
CXXOBJ = $(patsubst src/%.cpp,bin/%.o,$(CXXFILES))
INCLUDE = -I/usr/include/dlib-18.18
LIBS = `pkg-config --libs opencv`
CXXFLAGS = `pkg-config --cflags opencv` -w -Wall -Wextra -g -std=c++0x
LDFLAGS = -lcurl
CFLAGS = `pkg-config --cflags …Run Code Online (Sandbox Code Playgroud) 我想测量make文件中每个构建项目的时间,我只是在下面尝试,为什么它不起作用?
mytest:
$(info 'Now time is $(date --iso=seconds)')
日期不打印,只打印
'Now time is '.有什么不对?
make --version
GNU Make 3.81
经过几年的概念验证后,我又回到了C++.我有一个定义类的hpp文件,一个带有类方法的cpp文件和一个用于测试的main.cpp.我正在尝试创建一个在自己的线程中运行的tcp服务器(仅调用一次).我开始使用相同cpp文件中的所有代码并使其工作但是我现在得到了编译错误,因为我将类和方法放在他们自己的文件中.
我搜索过但没找到任何可行的东西.我尝试过使用extern,'singleton'等方法,这些都会导致各种错误消息.我知道我没有提供正确的方法参考.
tcpserver.hpp:
#ifndef __TCP_SERVER_HPP_INCLUDED__
#define __TCP_SERVER_HPP_INCLUDED__
#include <string>
class Server {
public:
static void *tcp_server(void * dummy);
static void hello();
static int parseCmd(const char *cmd, char *reply);
static int copystring(char *reply, const char *msg);
private:
};
#endif
Run Code Online (Sandbox Code Playgroud)
tcpserver.cpp,将类方法作为存根:
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
#include <cstring> // Needed for memset
#include <sys/socket.h> // Needed for the socket functions
#include <netdb.h> // Needed for the socket functions
#include <string.h>
#include "tcpserver.hpp"
int Server::parseCmd(const char *cmd, char …Run Code Online (Sandbox Code Playgroud)