在 Linux 中使用 makefile 包含 math.h 时无法编译 C 源文件

标签 c makefile

我正在尝试在 Linux 中使用 MPI 编译并行 C 程序。我知道我必须在编译器/链接器标志中包含 -lm ,以便正确包含 math.h 。

但是,我使用的是 makefile,无法获取包含 math.h 的 src 文件进行编译,即使我将 -lm 标志放入变量 CFLAGS 和 LFLAGS 中也是如此。它一直说 math.h 中定义的符号(M_PI、sin、cos、sqrt、...)未声明。

下面是我正在使用的 makefile。

# ------------------------------------------------
# Generic Makefile
#
# Author: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6cfd7d8dfd5dd98c4d9d5ded9d8f6d1dbd7dfda98d5d9db" rel="noreferrer noopener nofollow">[email protected]</a>
# Date  : 2011-08-10
#
# Changelog :
#   2010-11-05 - first version
#   2011-08-10 - added structure : sources, objects, binaries
#                thanks to http://stackoverflow.com/users/128940/beta
# ------------------------------------------------

# project name (generate executable with this name)
TARGET   = vdynamics

CC       = mpicc
# compiling flags here
CFLAGS   = -std=c99 -Wall -I.

LINKER   = mpicc -o
# linking flags here
LFLAGS   = -Wall -lm

# change these to set the proper directories where each files should be
INCDIR   = includes
SRCDIR   = src
OBJDIR   = obj
BINDIR   = bin

SOURCES  := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(INCDIR)/*.h)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm       = rm -f


$(BINDIR)/$(TARGET): $(OBJECTS)
    @$(LINKER) $@ $(OBJECTS) $(LFLAGS)
    @echo "Linking complete!"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
    @$(CC) $(CFLAGS) -c $< -o $@
    @echo "Compiled "$<" successfully!"

.PHONEY: clean
clean:
    @$(rm) $(OBJECTS)
    @echo "Cleanup complete!"

.PHONEY: remove
remove: clean
    @$(rm) $(BINDIR)/$(TARGET)
    @echo "Executable removed!"

编辑:

我收到的错误消息:

src/vtx_vtx_force.c: In function 'vdyn_vtx_vtx_force':
src/vtx_vtx_force.c:30:20: error: 'M_PI' undeclared (first use in this function)
     double pi_Lx = M_PI/Lx;
                    ^
src/vtx_vtx_force.c:30:20: note: each undeclared identifier is reported only once for each function it appears in
make: *** [obj/vtx_vtx_force.o] Error 1

我正在尝试编译的源文件:(我能够在Mac OS中编译它,当我在Linux中尝试时出现问题)

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <assert.h>

#include "vortex_dynamics.h"

void vdyn_vtx_vtx_force(vdyn_force_t F[], const vdyn_vortex_t vtx[], const vdyn_simulation_box_t* box, size_t start, size_t stop)
{
    assert(box != NULL);
    assert(start >= 0);
    assert(stop < box->nVtx);
    assert(stop >= start);

    double Lx = box->xLength;
    double Ly = box->yLength;

    // as variaveis comecando com t sao termos
    // das equacoes a serem resolvidas.
    double tcos = 0;
    double tsin = 0;
    double tcosh = 0;
    double tsinh = 0;
    double ttemp = 0;
    double rx_Lx = 0;
    double ry_Ly = 0;

    // os 2 proximos termos não dependem dos
    // vortices, apenas da caixa de simulacao.
    double pi_Lx = M_PI/Lx;
    double twopi_times_Ly_Lx = 2*M_PI*Ly/Lx;

    double Fx = 0;
    double Fy = 0;
    size_t nVtx = box->nVtx;
    size_t i = 0;
    size_t j = 0;
    int k = 0;

    // lembrando que f_index = 0 corresponde ao vortice em que
    // indice = start no vetor de vortices.
    size_t f_index = 0;

    for (i = start; i <= stop; ++i)
    {
        F[f_index].Fx = 0;
        F[f_index].Fy = 0;
        Fx = 0;
        Fy = 0;
        for (j = 0; j < nVtx; ++j)
        {
            if (j != i)
            {
                // resolvendo as equacoes por partes. Alguns termos
                // independem de k. Por isso, podemos calcula-los
                // antes de "for (k = ...)".
                rx_Lx = (vtx[i].x - vtx[j].x)/Lx;
                ry_Ly = (vtx[i].y - vtx[j].y)/Ly;

                // calculo dos termos com seno e cosseno (numerador
                // de Fx e denominador de Fx/Fy).
                ttemp = 2*M_PI*rx_Lx;
                tsin = sin(ttemp);
                tcos = cos(ttemp);
                for (k = -VDYN_NBOXES; k <= VDYN_NBOXES; ++k)
                {
                    ttemp = twopi_times_Ly_Lx*(ry_Ly+(double)k);
                    tcosh = cosh(ttemp);
                    tsinh = sinh(ttemp);
                    ttemp = tcosh - tcos;
                    Fx += tsin/ttemp;
                    Fy += tsinh/ttemp;
                }
                Fy -= 2*ry_Ly;
            }
        }

        // O termo que multiplica o somatorio é comum a todas as
        // contribuicoes. Por isso, pi/Lx foi colocado em evidencia.
        Fx *= pi_Lx;
        Fy *= pi_Lx;

        F[f_index].Fx = Fx;
        F[f_index].Fy = Fy;
        ++f_index;
    }

    assert(f_index == (stop-start+1));
}

谢谢!

最佳答案

M_PI 不是标准宏,并且看起来您的实现中不存在它。您可以自己定义它。

关于在 Linux 中使用 makefile 包含 math.h 时无法编译 C 源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176239/

相关文章:

c++ - 如何从 MS VS 中的大量 .c 和 .h 文件编译静态库

c++ - 在c++中将pcap文件解析为sip消息

c++ - 是否有可能在其工作后将线程连接到 'parallel for' 区域?

c - 解释 shellcode

c - sprintf 正在改变 bool 值

c++ - 在编译期间包含未使用的代码

makefile - cmake 用于具有通用代码的更大项目

转换为文件指针

c++ - 在 OpenWrt Makefile 中链接 pcap 库

c++ - G++ 如何编译与 .cpp 位于不同目录中的 .h