multiple-definition-error

multiple definition linker error after adding a function to a previously linking file

左心房为你撑大大i 提交于 2021-02-06 10:54:40
问题 So my program is working fine. Compiling, linking, running, the works. Then, I decide to add a simple function to one of my files, like this: #ifndef UTILITY_HPP #define UTILITY_HPP /* #includes here. There's no circular include, I've checked. */ namespace yarl { namespace utility { (several function declarations and definitions) bool isVowel(const char c) { if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; else return false; } } } #endif That function definition is

Multiple definition of first defined here gcc

寵の児 提交于 2021-02-04 14:29:50
问题 I have these files consumer.cpp consumer.hpp defines.hpp main.cpp makefile producer.cpp producer.hpp here's the file defines.hpp #ifndef DEFINES_HPP #define DEFINES_HPP #include <cassert> #include <pthread.h> #include <queue> #include <stdlib.h> #include <string> #include <unistd.h> pthread_mutex_t set_queue_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; std::queue<int> q; #endif // DEFINES_HPP This defines.hpp file is included in the producer.hpp

gcc __attribute__((selectany)) alternative for linux?

拈花ヽ惹草 提交于 2021-01-27 13:23:42
问题 i would like to know if there is an alternative for __attribute__((selectany)) in linux ? i would like to define something like that: char * a[] = { "qwe", "zxc" }; in a header file and include it in many .c files which would be linked together. so the linker will see more than one definition of "a" and therefor will not link. i've read of this attribute (selectany), which will use only the first seen definition of "a", unfortunately it is only for ms windows. so the question is: is there an

code guards fail and template from string literal

独自空忆成欢 提交于 2020-01-23 18:59:25
问题 I know the only way to pass a string literal as template argument is to declare it before: file a.h #ifndef A_H #define A_H #include <string> char EL[] = "el"; template<char* name> struct myclass { std::string get_name() { return name; } }; typedef myclass<EL> myclass_el; #endif file a.cpp #include "a.cpp" main.cpp #include "a.h" ... g++ -c a.cpp g++ -c main.cpp g++ -o main main.o a.o and I got: a.o:(.data+0x0): multiple definition of `EL' main.o:(.data+0x0): first defined here collect2: ld

Multple c++ files causes “multiple definition” error?

拈花ヽ惹草 提交于 2020-01-05 13:08:52
问题 I'm using multiple C++ files in one project for the first time. Both have need to include a protected (#ifndef) header file. However, when I do that, I get a multiple definition error. What I have is two one .cpp file that calls the header directly, and one indirectly (Another include includes it) and then two other header files that include it. So what do I need to do to get rid of the error? ERROR: obj\Debug\main.o||In function Z14sortLibQtyTest4BookS_':| [PATH]\miscFuncs.h|16|multiple

Multple c++ files causes “multiple definition” error?

有些话、适合烂在心里 提交于 2020-01-05 13:08:32
问题 I'm using multiple C++ files in one project for the first time. Both have need to include a protected (#ifndef) header file. However, when I do that, I get a multiple definition error. What I have is two one .cpp file that calls the header directly, and one indirectly (Another include includes it) and then two other header files that include it. So what do I need to do to get rid of the error? ERROR: obj\Debug\main.o||In function Z14sortLibQtyTest4BookS_':| [PATH]\miscFuncs.h|16|multiple

Multple c++ files causes “multiple definition” error?

烂漫一生 提交于 2020-01-05 13:08:30
问题 I'm using multiple C++ files in one project for the first time. Both have need to include a protected (#ifndef) header file. However, when I do that, I get a multiple definition error. What I have is two one .cpp file that calls the header directly, and one indirectly (Another include includes it) and then two other header files that include it. So what do I need to do to get rid of the error? ERROR: obj\Debug\main.o||In function Z14sortLibQtyTest4BookS_':| [PATH]\miscFuncs.h|16|multiple

How to avoid multiple definition linking error?

左心房为你撑大大i 提交于 2020-01-02 02:20:31
问题 Beside moving the hello() function into another source (.cpp) file or renaming the function. Is there any other methods to avoid the linking error? staticLibA.h #ifndef _STATIC_LIBA_HEADER #define _STATIC_LIBA_HEADER int hello(void); int hello_staticLibA_only(void); #endif staticLibA.cpp #include "staticLibA.h" int hello(void) { printf("\nI'm in staticLibA\n"); return 0; } int hello_staticLibA_only(void) { printf("\nstaticLibA: hello_staticLibA_only\n"); return 0; } output: g++ -c -Wall -fPIC

multiple definition of main first defined here

爱⌒轻易说出口 提交于 2019-12-31 06:59:31
问题 I'm new to programming and currently I'm learning C programming. I'm writing codes on the code blocks and in it using GCC compiler. When I create a new project, (as you know it creates main.c file with it) and due to that I'm not able to compile another file in that project. File 1: #include<stdio.h> int main() { int a,b,c,d; printf("Enter three numbers\n"); scanf("%d%d%d",&a,&b,&c); d=a; if(b>d) d=b; if(c>d) d=c; printf("\n The maximum of three numbers is %d",d); } File 2: main.c #include

extern variable causes multiple definition error

▼魔方 西西 提交于 2019-12-30 21:37:13
问题 I have been trying to use extern in order to use variable that is previously defined. I have not used extern before and now I need to use it in order to define variable just once and use them across multiple files I have written minimized version of code for this question. I have four files lib.h #ifndef LIB_H #define LIB_H #include <iostream> namespace lib { extern bool initialized; bool initialized = false; static void isInit(char* parent) { std::cout << "Library for [" << parent << "]