小编tim*_*kes的帖子

Cmake在单独的文件C++中链接一个类时遇到了麻烦

我对linux开发很新,并且在我的主要文件中使用来自单独文件的类时遇到了麻烦.我在制作时(在用cmake创建makefile之后)得到的错误是系统没有命名类型,我认为系统类中的代码是正确的,好像我编译而不试图创建系统类的对象我没有错误,所以我认为这可能与我编写CMakeLists.txt文件的方式有关.

这是我的CMakeLists文件:

    cmake_minimum_required (VERSION 2.6)
    project (GL_PROJECT)

    add_library(system system.cpp)

    include_directories(${GL_PROJECT_SOURCE_DIR})
    link_directories(${GL_PROJECT_BINARY_DIR})

      find_package(X11)

      if(NOT X11_FOUND)
        message(FATAL_ERROR "Failed to find X11")
      endif(NOT X11_FOUND)

       find_package(OpenGL)
      if(NOT OPENGL_FOUND)
        message(FATAL_ERROR "Failed to find opengl")
      endif(NOT OPENGL_FOUND)

    set(CORELIBS ${OPENGL_LIBRARY} ${X11_LIBRARY})

    add_executable(mainEx main.cpp system.cpp)

    target_link_libraries(mainEx ${CORELIBS} system)
Run Code Online (Sandbox Code Playgroud)

我在源目录中有我的main.cpp,system.h(类定义)和system.cpp(类实现)

主要:

      #include"system.h"


      system sys;

      int main(int argc, char *argv[]) {

      while(1) 
        {
            sys.Run();
        }

      }
Run Code Online (Sandbox Code Playgroud)

X11和GL包括在system.h中,我认为那里的代码是正确的,并没有导致错误(因为如果我不尝试创建类的实例,它构建正常).为了简洁,我省略了实际的标题和实现,希望它在CMakeList文件中是一个明显的错误,但如果有必要我也可以添加它们?

有任何想法吗?

提前致谢.

编辑:这是终端中的错误

    [tims@localhost build]$ make
    Scanning dependencies of target system
    [ 33%] Building CXX object CMakeFiles/system.dir/system.cpp.o
    Linking CXX static library libsystem.a
    [ …
Run Code Online (Sandbox Code Playgroud)

c++ linux types class cmake

3
推荐指数
1
解决办法
3331
查看次数

在GCC上构建函数多定义链接器错误 - Makefile问题

所以我试图使用Makefile来构建一个项目,而且我对makefile一般来说相对较新.我在连接函数时遇到了多个定义错误,我很确定它是由于我的makefile.我不能发布大部分项目,因为它非常大,但makefile在下面,有什么突出显然是错误的吗?

我在头文件中声明了+定义了一些函数,并将它们的定义移动到cpp中从链接器错误中删除了这些函数 - 但是我不能对所有这些函数执行此操作(编辑:其他正被多次定义的函数不是在标题中,它们在cpp/cc文件中作为标准,说"我不能为所有这些文件执行此操作"暗示它们都是那样的,抱歉),因为很大一部分是我无法编辑的代码库.代码中不应该有任何错误,因为它在没有我添加的单独项目中构建正常(没有一个导致链接器错误),所以我认为它必须是我的makefile,但我无法弄清楚我做错了什么.有任何想法吗?

    # Compiler
    CXX = g++

    # Linker settings
    LDFLAGS = -lGL -lGLU -lXext -lX11        

    # Executable name
    EXEC = SplotchPreviewer

    # Optimizations for compilation
    OPTIMIZE = -std=c++98 -pedantic -Wno-long-long -Wfatal-errors -Wextra -Wall -Wstrict-aliasing=2 -Wundef -Wshadow -Wwrite-strings -Wredundant-decls -Woverloaded-virtual -Wcast-qual -Wcast-align -Wpointer-arith -O2 -g


    # Pre-processor settings
    CPPFLAGS = $(OPTIMIZE) -I. -Icxxsupport -Ic_utils

    # Default Splotch objects
    OBJS_SPLOTCH_DEFAULT =  cxxsupport/error_handling.o reader/mesh_reader.o cxxsupport/mpi_support.o cxxsupport/paramfile.o \
                    cxxsupport/string_utils.o cxxsupport/announce.o reader/gadget_reader.o reader/millenium_reader.o \
                    reader/bin_reader.o reader/tipsy_reader.o splotch/splotchutils.o splotch/scenemaker.o \
                    cxxsupport/walltimer.o c_utils/walltime_c.o booster/mesh_creator.o …
Run Code Online (Sandbox Code Playgroud)

c++ gcc makefile definition

2
推荐指数
1
解决办法
7835
查看次数

标签 统计

c++ ×2

class ×1

cmake ×1

definition ×1

gcc ×1

linux ×1

makefile ×1

types ×1