繁体   English   中英

架构 x86_64 + 库的未定义符号未链接

[英]Undefined symbols for architecture x86_64 + library isn't linked

我很难理解为什么我会遇到编译错误。 同样由于某种原因,我的 target_link_libraries() 无法识别(查看 main.cpp)。 这是我的编译错误:

    ====================[ Build | phantom_omni_BA | Debug ]=========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/alexanderalbrecht/CLionProjects/phatom_omni_BA/cmake-build-debug --target phantom_omni_BA -j 3
[2/2] Linking CXX executable src/app/phantom_omni_BA
FAILED: src/app/phantom_omni_BA 
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  src/app/CMakeFiles/phantom_omni_BA.dir/main.cpp.o -o src/app/phantom_omni_BA   && :
Undefined symbols for architecture x86_64:
  "generateCode(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in main.cpp.o
  "loadSettings(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in main.cpp.o
  "saveSettings(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<int, std::__1::allocator<int> >)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

这是我的文件

CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
project(phatom_omni_BA)

set(CMAKE_CXX_STANDARD 14)

if(MSVC)
    add_compile_options(/W4)
else()
    add_compile_options(-Wall -Wextra -pedantic)
endif()

add_subdirectory(src)

src(目录)

CMakeLists.txt

add_subdirectory(app)
add_subdirectory(libSettings)

应用程序(目录)

主文件

#include <iostream>
#include <vector>
#include "../libSettings/saveSettings.h"
#include "../libSettings/loadSettings.h"
#include "../libSettings/generateSubjectCode.h"


using std::vector;

int main() {

    vector<int> bspArray = {1, 2, 3, 4, 5};
    saveSettings("Experiment9-Proband2-09:11", bspArray);
    loadSettings("Experiment7-Proband2-09:11");
    cout << generateCode("Experiment1");
    return 0;
}

CMakeLists.txt

add_executable(phantom_omni_BA main.cpp)

libSettings(目录)

CMakeLists.txt

add_library(Settings datetime.cpp generateSubjectCode.cpp loadSettings.cpp saveSettings.cpp)

target_link_libraries(Settings PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

日期时间.h

#ifndef PHATOM_OMNI_BA_DATETIME_H
#define PHATOM_OMNI_BA_DATETIME_H

#include <sstream>
using std::string;

const string currentDateTime();

#endif //PHATOM_OMNI_BA_DATETIME_H

日期时间.cpp

#include "datetime.h"


/**
 * Get current date/time, format is YYYY-MM-DD.HH:mm:ss
 * @return
 */
const string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d-%X", &tstruct);

    return buf;
}

生成主题代码.h

#ifndef PHATOM_OMNI_BA_GENERATESUBJECTCODE_H
#define PHATOM_OMNI_BA_GENERATESUBJECTCODE_H

#include <sstream>

using std::string;

int subjectNumber = 0;

string generateCode(string experimentName);

#endif //PHATOM_OMNI_BA_GENERATESUBJECTCODE_H

生成主题代码.cpp

#include "generateSubjectCode.h"
#include "datetime.h"

/**
 * Generates code snippet
 * @param experimentName
 * @return code this is the code snippet
 */
string generateCode(std::string experimentName) {
    string code = " " + experimentName + "-" + "Proband" + to_string(subjectNumber) + "-" + currentDateTime();
    subjectNumber++;
    return code;
}

加载设置.h

#ifndef PHATOM_OMNI_BA_LOADSETTINGS_H
#define PHATOM_OMNI_BA_LOADSETTINGS_H

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using std::string;
using std::cout;
using std::vector;
using std::stringstream;
using std::fstream;
using std::ios;

vector<vector<string>> loadSettings(string fname);

#endif //PHATOM_OMNI_BA_LOADSETTINGS_H

加载设置.cpp

#include "loadSettings.h"

/**
 * Searches through directory
 * If settings have been already created, then load those files
 * @param fname
 * @return
 */
vector<vector<string>> loadSettings(string fname) {


    vector<vector<string>> content;
    vector<string> row;
    string line, word;

    fstream file (fname, ios::in);
    if(file.is_open())
    {
        // Get line of csv file
        while(getline(file, line))
        {
            row.clear();

            stringstream str(line);

            // Get element for element, separated through ","
            while(getline(str, word, ',')) {
                row.push_back(word);
            }
            content.push_back(row);
        }
    }
    else {
        cout<<"Could not open the file\n";
    }

    return content;

    for(int i=0;i<content.size();i++)
    {
        for(int j=0;j<content[i].size();j++)
        {
            cout<<content[i][j]<<" ";
        }
        cout<<"\n";
    }
}

保存设置.h

#ifndef PHATOM_OMNI_BA_SAVESETTINGS_H
#define PHATOM_OMNI_BA_SAVESETTINGS_H

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using std::string;
using std::vector;
using std::ofstream;
using std::to_string;

void saveSettings(string subjectCode, vector<int> settingsArray);

#endif //PHATOM_OMNI_BA_SAVESETTINGS_H

保存设置.cpp

#include "saveSettings.h"

/**
 * Saves Settings of user
 * As CSV File
 * @param subjectCode
 * @param settingsArray
 */
void saveSettings(string subjectCode, vector<int> settingsArray) {
    // Create output filestream object
    ofstream myFile(subjectCode);

    // Fill Object with data
    for (int i = 0; i < settingsArray.size(); i++) {
        myFile << to_string(settingsArray[i]) + ",";
    }

    // Close file
    myFile.close();
}

这里也是我的配置:运行配置工具链

您将 lib 与其自身链接。

libSettings/CMakeLists.txt:

add_library(Settings datetime.cpp generateSubjectCode.cpp loadSettings.cpp saveSettings.cpp)

target_link_libraries(Settings PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

将最后一行移到 CMakeLists.txt

add_executable(phantom_omni_BA main.cpp)
target_link_libraries(phantom_omni_BA PRIVATE Settings)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM